SpaceRocket Takes Off: Implementing Infrastructure as Code with Cloud9 IDE (Part 1)

SpaceRocketIaC uses Cloud9 IDE, Terraform, and Ansible for Infrastructure as Code (IaC). This makes it easy to set up and maintain AWS infrastructure. Our goal is to make the code ready to be integrated into your CI/CD pipeline.

The easiest way to get started using IaC with AWS services is by using AWS's Cloud9 IDE.

This tutorial assumes familiarity with Terraform, Packer, Ansible and AWS.

Getting Started with Cloud9 IDE

Cloud9 is a browser based code editor that is hosted on an EC2 instance. AWS Cloud9 can serve as a 'bastion host' by providing a fortified gateway to AWS services. It provides a fortified gateway to AWS services through the use of AWS best practices such as credential rotation and session keys, as well as the ability to limit access by IP addresses. AWS Cloud9 also simplifies IaC (infrastructure as code) management by providing pre-authentication to AWS services, which allows for easy modification and deployment using tools like Terraform, Packer, and Ansible.

In this tutorial, we will be setting up Cloud9 IDE, associating an Elastic IP address, and installing Terraform, Packer and Ansible. This tutorial assumes you are already logged into the AWS console with a non-root user with administrative access. If not, follow these instructions: Getting set up with IAM.

Without further ado, lets get started!

1. Search for Cloud9 in the AWS search bar, then create a new environment by clicking the "Create environment" button.

2. Give your environment a name and select the environment type.

Give your environment a name and select the environment type.

3. Select the desired instance type (for demonstration purposes, t2.micro should be sufficient. If you need more resources, choose a larger instance type), Ubuntu Server 18.04 LTS as your platform, and lastly, choose how long you want your Cloud9IDE to be active. This is useful because you will not be charged for the time when the Cloud9IDE is not in use (but is also why we need to set up an Elastic IP, more on that later).

Select the desired instance type (for demonstration purposes, t2.micro should be sufficient. If you need more resources, choose a larger instance type), Ubuntu Server 18.04 LTS as your platform, and lastly, choose how long you want your Cloud9IDE to be active. This is useful because you will not be charged for the time when the Cloud9IDE is not in use.

4. Select Secure Shell (SSH) for the connection method for your environment. You can leave the VPC and Subnet as their default values.

Select Secure Shell (SSH) for the connection method for your environment. You can leave the VPC and Subnet as their default values.

5. Lastly, click the "Create" button to create the environment.

Click the "Create" button to create the environment.

6. Once the environment is ready, click Open in the Cloud9IDE column, you will then be taken to the Cloud9 IDE web-based code editor.

Once the environment is ready, click Open in the Cloud9IDE column, you will then be taken to the Cloud9 IDE web-based code editor.

7. From the terminal, you can run commands and interact with the environment just as you would on a local terminal.

From the terminal, you can run commands and interact with the environment just as you would on a local terminal.

Now that we have created the Cloud9 instance and environment, we are ready to create an Elastic IP and associate it with the Cloud9 instance. After that, we will install Terraform, Packer and Ansible.

Set Cloud9 Elastic IP

We need to add an Elastic IP to the Cloud9 instance to maintain consistant access, even after restarts and the IP address of the EC2 instance changes. Let's do that by creating an Elastic IP and associating it with the Cloud9 instance.

Go to the EC2 service in the search bar and navigate to Elastic IPs via the left navigation menu.

Search for EC2

Click "Allocate Elastic IP address". On the next screen stick with default Amazon's pool of IPv4 addresses and give a Name tag of Demo Cloud9 EIP and then click "Allocate".

Select default Amazon's pool of IPv4 addresses and give a Name tag of Demo Cloud9 EIP and then click "Allocate".

On the next screen select the Cloud9 EIP address and click the "Actions" drop down button and click "Associate Elastic IP" (or click the button that says "Associate this Elastic IP address" in the big green banner).

Click "Associate Elastic IP" (or click the button that says "Associate this Elastic IP address" in the big green banner)

On the next screen select Instance as the resource type and the DemoCloud9IDE instance from the drop down selector and click "Associate".

Now we have a static IP address that won't change after the Cloud9 instance shuts down from inactivity and restarts later with a different IP address, it will use the Elastic IP address instead.

Getting your Elastic IP

To obtain your Elastic IP, navigate to the EC2 service in the AWS console, then on the left sidebar, go to Network & Security > Elastic IPs. Select your Cloud9 EIP and under the 'Summary' tab, copy the "Allocated IPv4 address". Keep in mind that when using it in CIDR notation, it will be in the format '<IP.AD.DR.ESS>/32'.

Install Terraform, Packer and Ansible

Before you can use the Terraform and Packer tools on our brand spanking new Cloud9 IDE, we need to install them on the system.

How to install Terraform on Ubuntu

You can find the official Terraform install steps at https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli Under the Linux > Ubuntu/Debian tab.

How to install Packer on Ubuntu

Since we already have Hashicorp's Linux repository on our system, we can run the following command to install Packer:

Terminal
sudo apt-get update && sudo apt-get install packer

How to install Ansible on Ubuntu

Note you may need to update 'pip' to latest version.

Terminal
sudo -H pip3 install --upgrade pip

Once you have completed installing Terraform and Ansible, confirm they are installed.

Terminal
terraform --version
Terminal
packer --version
Terminal
ansible --version

Congratulations! Your Cloud9 IDE is now set up and ready for use

That is it! In this tutorial, we set up a Cloud9 IDE that acts as a bastion for our AWS services and IaC tools. We installed Terraform, Packer, and Ansible, and verified their versions. We also set up an Elastic IP, so that it can be used in the future for tools such as Ansible to SSH into EC2 instances created by Terraform. We are now ready to start "Building Infrastructure with Terraform and Ansible."