Subscribe to Our Weekly Newsletter
Modifying the Application
Modifying the Application
We are now ready to modify the application in order to publish our images to Amazon S3 automatically. We will also use Amazon RDS to index our content which is stored in Amazon S3.
Note: We will be modifying the code from the web server, so that the webmaster’s S3 credentials are not exposed to end users.
We will be modifying the application in the following ways:
- Updating your web.config file
- Modifying your code so that it points to your Amazon CloudFront distribution or Amazon S3 bucket
- Modifying your code so that it points to your Amazon RDS file
Here’s the procedure to update your web.config file:
- Open the web.config file, and replace the text that says “Enter your access/secret key here” with the actual values.
<appSettings> <add key=”PublicKey” value=”Enter your access key here”/> <add key=”SecretKey” value=”Enter your secret key here”/> </appSettings> |
Note: In production, we would always encrypt web.config, but that would be out of scope for this tutorial exercise. Furthermore, it is always a good practice to use one’s AWS Identity and Access Management (IAM) user accounts, instead of the master credentials.
Here is the procedure to modify the code so that it points to your Amazon CloudFront distribution or Amazon S3 bucket:
- Open “default.aspx.cs” on your Amazon EC2 instance, and replace “http://your-non-dotted-bucket-name.s3.amazonaws.com/” with the name of your Amazon CloudFront distribution or Amazon S3 bucket. (For ex: In the section Creating a CloudFront Distribution, our Amazon CloudFront distribution is d18k4jybr69gw2.cloudfront.net.)
- Open “default.aspx.cs” and replace “your-non-dotted-bucket-name” with the name of your Amazon S3 bucket. “For ex: In the section Creating an Amazon S3 Bucket, our Amazon S3 bucket is webapplication.)
Note: You should ensure that you don’t forget the trailing slash for your Amazon S3 bucket or your Amazon CloudFront distribution.
public partial class _Default : System.Web.UI.Page { public string PhotoGalleryBaseUrl = “http://d18k4jybr69gw2.cloudfront.net/”; … private const string ImageBucketName = “webapplication”; } |
If you had skipped creating the CloudFront distribution, you will have to enter your Amazon S3 bucket, as shown in the following example:
public partial class _Default : System.Web.UI.Page { public string PhotoGalleryBaseUrl = “http://webapplication.s3.amazon aws.com/”; … private const string ImageBucketName = “webapplication”; } |
Note: This code will publish images to your Amazon S3 bucket and set the permissions on each file to be public (readable by everyone).
Here’s the procedure to modify the code so that it points to your Amazon RDS database:
- Open “default.aspx.cs” and replace the text for the following variables with the actual values. These values are used to build your connection string using MySQL Connector/.NET.
- Database endpoint
- Database master user ID
- Database master password
- Database name
public partial class _Default : System.Web.UI.Page { private string dbinstance = “mydbinstance.cgwxy4t1e0xb.us-east- 1.rds.amazonaws.com”; private string userid =”awsuser”; private string password =”mypassword”; private string database =”mydb”; } |
It is time to deploy your sample website now.
Follow these steps to restart your website:
- Navigate to “Default Web Site” in the IIS Manager, and right-click on it.
- Select “Manage Web Site” and click on “Restart”.
- Find the DNS address of the load balancer:
- In the AWS Management Console, click on the “Amazon EC2” tab.
- In the “Navigation” pane, click on “Load Balancers”.
- In the “Load Balancers” pane, click on “MyLB”, and note the public DNS in the bottom pane.
- Now, you need to type the DNS address of the load balancer in your web browser to verify that your application is still working.
- There are no images in the current Amazon RDS database, and so there will be no images seen. You should upload images (less than or equal to 4 MB) to the website, and check whether they are being displayed correctly.
Note: You may not be connected to your Amazon RDS database if you’re having trouble in displaying the images. Ensure that you’ve typed your Amazon EC2 security group correctly while authorizing access for the database security group. You should try to re-authorize your Amazon EC2 security group if the problem persists.
Congratulations! You have been successful in deploying a sample web application using Amazon Web Services. Now, if you decide you want to launch more instances in the future, there is no need to customize each one.
Let us now move on, and create a custom Amazon Machine Image (AMI) with all the configuration changes we’ve just made.