How to Automate AWS Provisioning with Terraform Using Claude

Ms. BMs. B
4 min read

I remember joining Sulaiman Olubiyi first webinar where he was teaching Implementation of IAM and VPC using Terraform; even when I did not really understand what he was doing, I stayed in the webinar because I knew I was going to learn something and that somehow, I was going to have to learn to use Terraform if I wanted to be a Devops Engineer. So while strolling through the streets of LinkedIn and came across the Multicloud, Devops & AI 5 Day challenge by Jean Rodrigues, I knew I had to jump on it.

This week, I would be actively participating in this challenge and would be giving updates on how I am going with the challenge.

For the purpose of this challenge, I would be creating a IAM Role for our EC2 instance and giving AdministrativeAccess Policy to this role. This will help me have access to any applications I would be working with, and I would not need to create access keys.

  1. CREATE A IAM ROLE

    a. Open the AWS Console - Go to IAM - Click on Role - Select Create Role

b. Choose AWS Service

c. Under Service or Use Case, choose EC2 in the drop box and select Next.

d. Under Add Permissions, Choose AdministrativeAccess and select Next.

e. Under Role name, give the role any desired name. I gave EC2Admin as Role name. Next, click Create Role.

f. The role has been created in the diagram as shown below.

  1. LAUNCH AND CONNECT EC2 INSTANCE

    a. I would be launching an EC2 Instance. You can check my previous post Quick Guide to Deploying a Linux EC2 Instance on AWS to guide you on how to launch an instance.

    b. I have launched the instance and would connect it using EC2 Instance Connect.

c. After connecting the instance, I ran the command sudo yum update -y to update the Linux kernel.

d. Next, I ran sudo yum install -y yum-utils.

e. Next, I configured a repository belonging to a company named Hashicorp on the instance by running the command sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

  1. INSTALL TERRAFORM ON EC2 INSTANCE

a. I installed Terraform using sudo yum -y install terraform on the instance.

b. Next, I would be creating my first resource which would be an S3 bucket on AWS using Terraform but before that, I created a folder or directory where I would like to keep my terraform files using the mkdir terraform-project && cd terraform-project command. I also created a file and named it demo.tf

c. I opened the Terraform documentation on my browser and copied the script I needed to create an S3 bucket which is the resource I want but to make it easier and help me work smarter, I would be using an AI assistant named CLAUDE.

d. I opened a new browser and typed claude.ai and asked the AI to write a code for me “Act as a Terraform and AWS expert and help me create a configuration file to provision a S3 bucket on AWS. Give it a unique name and let the region be us-west-1”. I pasted the code in the file I created, saved it and returned to the instance environment.

e. Next, I would run terraform init to initialize terraform and download all the plugins.

i. terraform plan command would look at what we have in the file and let you know what terraform intends to do before creating the resource.

j. When I ran the above command, I had an error as shown below and I fixed it.

NB: Make sure to attach your IAM Role to the instance else you would have errors when trying to run the terraform plan command.

I did not attach the IAM Role to my instance so I had to go back and do it.

I chose my instance and followed the steps in the diagrams below to attach the IAM Role to my instance.

k. I ran the terraform plan command again after fixing the issue above.

l. Run terraform apply to create the resource in the cloud. After I ran the command, I did not need to input any access key because I already have the AdministrativeAccess privilege attached to the IAM Role. I only had to type YES for approval and the S3 Bucket was created in 1 sec.

m. You can go to the AWS Console and you would see the S3 bucket that has been created for you by Terraform.

  1. CREATE CLOUD DYNAMODB TABLES

    I copied the code I needed to create the tables I needed for this challenge, put it in my demo.tf file which I earlier created by editing it. Returned to the EC2 connection, typed terraform applyand ran it. You can always run terraform plan first if you are not sure of what terraform would do.

    Stay tuned and follow me through this challenge. Feel free to share your thoughts or ask questions in the comments, I’d love to hear from you!

Thank you and see you on Day 2 of the challenge.

1
Subscribe to my newsletter

Read articles from Ms. B directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ms. B
Ms. B

Hi, I'm a tech enthusiast who has decided to document her cloud journey as the day goes by. Stay tuned and follow me through this journey which I believe would be a wonderful experience. I'm also a team player who loves collaborating with others to create innovative solutions.