Terraforms : Practical guide to creating EC2 and DynamoDB
In the previous blog, I explained all the core concepts and syntax of Terraform. Here we will create a Terraform configuration to instantiate an EC2 serveer and DynamoDB table, which means we will use AWS as our Provider and EC2, Dynamo as the Resource.
We are going to use 2 important components of Terraform to do this activity :
- Provider — Providers help you create and manage your resources in different infrastructures like AWS, GCP.
- Resource — Resource acts like a basic building block of an infrastructure. Servers, subnets, VPCs, Databases are all considered as resources.
Follow the below steps to create DynamoDB table and EC2 :
Step 1 :
Installation of Terraform — Download Terraform according to your OS specification, extract the zip and add this to your PATH variable.
Step 2 :
An IAM User with a policy to perform CRUD operations on DynamoDB and EC2.
We will need an AWS account. Create an IAM user like below and download the access key id and secret access key because we will need them to be passed as parameters to connect to AWS.
Make sure to attach the AdministratorAccess policy to your user . I got an error related to this so just thought of mentioning it here. Screenshot below :
Step 3:
Create a main.tf file and copy these contents mentioned below :
Use an AWS Provider and pass your access_key, secret_key and region.
#Using AWS as infrastructure
provider “aws” { access_key = “ACCESS_KEY_HERE” secret_key = “SECRET_ACCESS_KEY” region = “us-east-2”}
Step 4 :
Create the Resource EC2
Find your ami id and instance type from the list and configure below . Remember that your ami image is region dependent, so make sure to recheck the region id and region specified in your resource body.
#creating EC2 resource#Instance types : https://aws.amazon.com/ec2/instance-types/#ami id : https://wiki.centos.org/Cloud/AWS#head-78d1e3a4e6ba5c5a3847750d88266916ffe6968resource “aws_instance” “EC2” { ami = “ami-0d97ef13c06b05a19” instance_type = “t2.micro”}
Step 5 :
Create a DynamoDB Resource
# Create DynamoDb resource https://console.aws.amazon.com/iam/home?region=us-east-2#/users$new?step=final&accessKey&login&userNames=vivek_singh&passwordReset&passwordType=autogenresource “aws_dynamodb_table” “basic-dynamodb-table” { name = “GameScores” billing_mode = “PROVISIONED” read_capacity = 20 write_capacity = 20 hash_key = “UserId” range_key = “GameTitle” attribute { name = “UserId” type = “S” } attribute { name = “GameTitle” type = “S” } ttl { attribute_name = “TimeToExist” enabled = false } tags = { Name = “dynamodb-table-1” Environment = “development” }}
DynamoDB supports two types of primary keys — a Hash Key and combination of Hash Key and Range Key
- A Hash Key consists of a single attribute that uniquely identifies an item.
- A Hash and Range Key consists of two attributes that together, uniquely identify an item.
Attribute block creates different attributes commonly referred to as columns in the rdb world.
That’s it. We are done with coding. We just need to execute some terraform commands to get our infrastructure up and running.
Step 6 :
Run terraform init — Downloads the providers.
The screenshot of what actually is printed on console when the command is run :
Step 7 :
Run terraform plan — This will show all the CRUD operations that will be performed on your AWS infrastructure. Remember this is for you to understand the actions that will be performed, this does not actually create the resources.
Screenshot for Creation of DynamoDB :
Screenshot for Creation of EC2 :
Step 8 :
Run command terraform apply — will create resources in real time on AWS.
The logs that were printed on the console had larger content, I have added screenshots for a few.
We are done creating the resources in AWS infrastructure.
These are screenshots for EC2 and DynamoDB from AWS console (after executing terraform apply):
DynamoDB :
EC2 :
Directory contents after above steps are performed :
.terraform.lock.hcl maintains the versions for your provider so that the same provider configuration can be maintained across different environments.
Screenshot for the file content :
Terraform.tfstate is used to maintain the mapping of real world resources in your locale so that on any modification of the main.tf file or modification of resources, only the updates are applied in AWS infrastructure instead of destroying the entire infrastructure and creating them again. This is where Terraform wins over Chef and Ansible because it maintains state to determine already present configuration.
Check this blog for more info around the difference in Terraform and Chef/Ansible.
Let’s see what happens when we update any resource in the main.tf file. Here we are changing the ami image id to use a different version of CentOS.
New Code for EC2 resource. Other file content will remain same :
resource “aws_instance” “EC2” { ami = “ami-035734c938e7da7af” instance_type = “t2.micro”}
Run the command terraform plan . This will clearly show the modification needed in infrastructure to incorporate this ami image change.
If you check the screenshot above, it clearly depicts the changes needed using signs like + and — .
Run terraform apply to make the same changes on real time mapping i.e. your AWS infrastructure.
Meaning of mathematical symbols in Terraform plan :
+ create
- destroy
~ update in place
-/+ replace (destroy and then create or vice versa)
Conclusion :
This was a practical guide to Terraform. I have tried to add as many screenshots as I could and break down the implementation process into smaller steps, so that it is evident to users reading to actually understand it completely without writing any code.