Implementing Cloud Design Patterns for AWS(Second Edition)
上QQ阅读APP看书,第一时间看更新

Cloud9

Although we said everything is going to be code, and while it is possible to create our integrated development environment (IDE) as code, we are going to use the AWS console for this exercise. Log in to the console, click the Services button, and search or scroll to the Cloud9 service. This cloud native IDE is provided for us by Amazon, so we will use it in order to simplify our security and easily integrate with other AWS services. Go ahead and create an environment (press the big orange Create environment button):

If, when you access services, you receive a warning on the page about being logged in as a root user, you should consider adding in a less privileged user through the IAM service. IAM is covered later in this chapter.

Select the smallest available option (when we were writing this book, it was the t2.micro (1 GiB RAM + 1 vCPU) instance type) for now and keep all of the other defaults. Press the button at the bottom of the page for the Next step:

Check the settings on the Review step page and confirm; you should then eventually see something like the following screenshot. Creating environments takes a few minutes:

Right-click on the cloudpatterns folder and create a new folder called ch2:

Now we will install Terraform to help us to manage our infrastructure as code configuration. Click Create File and add the following code:

#! /bin/bash
mkdir download
cd download/
wget https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
unzip terraform_0.11.11_linux_amd64.zip
mv terraform /usr/bin/terraform
terraform
exit
HashiCorp releases new Terraform versions frequently. Use the latest version on the download page ( https://www.terraform.io/downloads.html) as it will contain bug fixes, new features, and security patches.
If you find that this script fails, the most likely reason is that you're using an IAM user. You will need to prepend lines 6 and 7 with sudo to make this work.

Save the file in your ch2 folder as terraform_install.sh, then click Run:

You will see some scrolling at the bottom of your window, then Terraform help will appear. You can close the Run window and click in the bash window:

Now that we have some source code, we need a place to keep it outside of Cloud9. We will create an AWS CodeCommit source repository using Terraform. Let's start with a new codecommit.tf file:

provider "aws" {
region = "us-east-1"
}
resource "aws_codecommit_repository" "cloudpatterns" {
repository_name = "cloudpatternsrepo"
description = "This is a demonstration repository for the AWS Cloud Patterns book."
}

I'm going to use spaces to indent my code. Some people use tabs.

We need to initialize the Terraform environment using terraform init first. This will configure the AWS provider for you. Then, you can run terraform apply:

Great! Let's put our code into our repository.

You could have also used the built-in AWS CLI to create your repository from your bash window. The command would have been:  aws codecommit create-repository --repository-name cloudpatternsrepo --repository-description "This is a demonstration repository for the AWS Cloud Patterns book." --region us-east-1.

Here is some example code for creating a Cloud9 environment with Terraform, create_env.tf:

resource "aws_cloud9_environment_ec2" "cloudpatterns" {
instance_type = "t2.micro"
name = "example-env"
automatic_stop_time_minutes = "30"
description = "My Cloud Patterns Environment"
}

Some of the code in this book is adapted from the AWS Terraform provider documentation under the Mozilla Public License 2.0. It is available at  https://github.com/terraform-providers/terraform-provider-aws/.