You can create your CloudFront Distribution from CLI, the AWS Console, and using APIs. However, for convenience at the first stage, here you will be creating using the console.
1. Go to the AWS services dashboard and select CloudFront from the listed services and click on the Create Distribution button:
[ 87 ]
2. CloudFront supports you to create static content as well as streaming content distributions. You are going to distribute blog-related content, so select the first option ("Download") and click on the Continue button:
3. Select your distribution's content origin. You need to specify the source of the content that you want in the Origin Domain Name field. As seen in the previous section, you have already created the S3 bucket, so your console will help you in that case to identify the bucket and its related Origin ID, which will just be the nickname assigned to the origin.
4. I recommend enabling the Restrict Bucket Access option to ensure your content is not accessible directly from S3 but via CloudFront only. This implies that you need to create an IAM identity and edit the bucket policies but Amazon will do it for you here, you only have to choose Create new identity and enable Yes, Update Bucket Policy:
[ 88 ]
5. Choose Default Cache Behavior Settings option. In this section, you can enforce HTTPS usage. You can also select the cache behavior by policy or in the content header. You can also forward cookies and URL query strings to your origin, or restrict access for your content using signed URLs:
[ 89 ]
6. Choose Distribution Settings. In this section, you have to select Price Class to select regions and edge locations where your content will be cached. The pricing is dependent on the regions and edge locations, so pick up the correct and appropriate locations based on your traffic analysis and requirement. You can even set an alias on your DNS to your CDN. For this, you have to declare name(s) as Alternate Domain Names (CNAMEs). Once done, click on Create Distribution button:
[ 90 ]
7. Once the distribution is in the Enabled state on the AWS console, you are almost done. You can check the settings in which you will be able to find the domain name for your content. Just note that you will have to tell the WordPress blog where your CDN will be located:
8. Now you have to create an IAM user who can manage your S3 bucket and assign a custom policy to it. Optionally, you can integrate Amazon S3 with Amazon CloudFront. For this, you will need the WordPress plugin, which is available on http://wordpress.org/plugins/amazon-s3-and- cloudfront/. So after the configuration and installation of this plugin, you will see a new segment with the CloudFront URL where the content will be served from.
To remove the content from the edge locations, you can use the invalidating functionality of CloudFront. For this, you have to do the following:
1. Traverse to the CloudFront section on the AWS console. 2. Choose your distribution.
3. Click on the options icon.
4. From here, move to the Invalidations tab and then Create Invalidation. 5. Place the names of the files to be invalidated.
6. Click on the Invalidate button.
[ 91 ]
Once this procedure is complete, the object cached on the edge locations will be removed and the latest version will be cached at the subsequent time it is requested.
Amazon RDS management with CLI
Amazon Relational Database Service (RDS) is a web service that makes it easy to set up, operate, and scale a relational database on the AWS Cloud platform. It has access to the full capabilities similar to MySQL, Oracle, MSSQL, PostgreSQL, and Aurora databases. So this means the code, applications, and tools you already use today with your existing databases work seamlessly with Amazon RDS as well. There are a number of advantages to Amazon RDS, which includes the following:
1. Accelerated deployment 2. Managed and scalable 3. Reliable and compatible
4. Automated backup and multi AZ deployment 5. Secure
To manage the Amazon RDS, you have to first set up tools for it. In our exercise, we will install tools and the setup environment, database instance creation and listing, and connect database instances. So let's gear up with the environment setup and configuration. To install tools, you need Java 1.6 or higher and the Amazon RDS command-line toolkit. To start with Java:
1. Set the Java variable in your system:
1. The command-line tool reads an environment variable (JAVA_HOME) on the machine to locate the Java runtime. Either JRE or JDK should be fine with the Version 6 or higher. To download Java, go to http://java.oracle.com/.
2. Extract your downloaded Java and set the path using variable JAVA_ HOME to the full path of the directory, which will contain the bin subdirectory. For example, if you have Java in the /opt directory, the path should be JAVA_HOME to /opt/jdk for Linux and C:\jdk for Windows.
3. To set JAVA_HOME, use the following commands: For Linux:
$export JAVA_HOME=/opt/jre
[ 92 ] For Windows:
C:\> set JAVA_HOME=C:\java\jdk1.6.0_6
4. Include the Java directory to your system path before other versions of Java:
For Linux:
$export PATH=$PATH;$JAVA_HOME/bin
For Windows:
C:\> set PATH=%PATH%;%JAVA_HOME%\bin
5. Verify your JAVA_HOME settings: For Linux:
$ JAVA_HOME/bin/java –version
For Windows:
C:\> %JAVA_HOME%\bin\java –version
2. Setting up the RDS CLI tools. To access the RDS command-line toolkit, you need to download it from the AWS site http://aws.amazon.com/ developertools/2928/ and set it up with your AWS credentials within the instance. You need to just download and unzip it. No installation is required; it will come as a zip bundle.
1. The CLI also depends on the environment variable, so we have to again set its path with the AWS_RDS_HOME variable:
For Linux:
$ export AWS_RDS_HOME=/usr/local/RDSCLI1.15.001 $ export PATH=$PATH:$AWS_AUTO_SCALING_HOME/bin
For Windows:
C:\> set AWS_AUTO_SCALING_HOME=C:\CLIs\ RDSCLI1.15.001 C:\> set PATH=%PATH%;%AWS_AUTO_SCALING_HOME%\bin
Your environment variables in the Windows machine may reset when you close the terminal window. You may want to set them permanently using the setx command same as set.
[ 93 ]
3. Authenticate your AWS account with RDS tools:
1. After signing in, you need to create access keys and secret keys for your account. You have to provide these keys to your CLI tools. You can create your AWS secret keys and access ID from https:// console.aws.amazon.com/iam/home?#security_credential. 2. Create a new file called CredentialFile and save your access
key ID and secret access key to the file.
3. Give 600 permission to that CredentialFile file if you are a Linux user.
$ chmod 600 CredentialFile
4. Set the AWS_CREDENTIAL_FILE variable based on your file location. Now, you are almost done with setting up the AWS RDS CLI tools. So you can test the tools on a Windows or Linux machine using the following command:
rds –-help
You should see the usage page for all Amazon RDS commands. If it shows results, that means you have done a good job of setting up an RDS toolkit. Now it's time to create and launch an instance from CLI for RDS.
The following is the screenshot for the RDS dashboard from which you can perform the same operations and manage your database on the AWS infrastructure:
[ 94 ]
Now, to start an RDS using CLI, follow below steps:
1. To launch an RDS instance from CLI, use the following command. This will create a database instance called uchitinstance with 5 GB of storage and an initial database named uchit:
rds-create-db-instance --engine MySQL5.3 --master-username root --master-user-password mypass --db-name uchit --db-instance-
identifier uchitinstance --allocated-storage 5 --db-instance-class db.m1.xlarge –-header
2. You can see the below output if everything is correct:
DBINSTANCE DBInstanceId Class Engine Storage Master Username
Status Backup Retention
DBINSTANCE uchitinstance db.m1.xlarge mysql5.3 5 root creating 1
SECGROUP Name Status SECGROUP default active
PARAMGRP Group Name Apply Status PARAMGRP default.mysql5.3 in-sync
3. To list the available instances, use the following command:
rds-describe-db-instances –-headers