VOOZH about

URL: https://dev.to/happyviki/iac-aws-cloudformation-stack-basics-3d88

⇱ IaC AWS CloudFormation Stack Basics - DEV Community


I'm almost done with my AWS Cloud Resume Challenge! Part of it is making a blog post about the process, but I wanted to make a separate post about Infrastructure as Code; this is new to me and I would like to understand it better.

Making a CloudFormation stack template is also part of the challenge, but I would have done this even if it wasn't. I think it's cool to be able to start fresh, and then automate infrastructure setup.

Choosing My Infra Automation Tool

At first I wanted to use Infrastructure Composer, but that was overwhelming; too many different buttons to click.

Then, I thought I would use IaC Generator. That was overwhelming and confusing because there are too many services, and I didn't know which ones to include.

Finally, I decided to go with CDK, since I'm already a programmer, I thought this would be easier for me.

AWS Cloud Development Kit

I was right, sort of. I needed some practice deploying something simple first. I used ChatGPT, YouTube, and Stack Overflow (yes, I still read it!) to figure out how to deploy a simple S3 Bucket Website.

I tried watching some one hour videos for a few minutes, but decided this wouldn't be very helpful for me, because I learn by doing, and I wanted the most simplest thing!

Lucky for me, I found just the guy! He explained what I wanted to do in 15 minutes, and I was able to follow along really well. He did C# (I can comprehend C#), so I used ChatGPT to help me fix my translated JavaScript code and deploy with aws cdk cli.

Getting Started With Infrastructure as Code (AWS CDK, CloudFormation)

My Code

Setup:

npm install -g aws-cdk
mkdir aws-resume
cd aws-resume
cdk init app --language javascript

Code for creating/deleting all in public website s3 bucket:

// Bucket name will be auto generated
const bucket = new s3.Bucket(this, 'ResumeBucket', {
 websiteIndexDocument: 'index.html',
 // Allow anyone to see this bucket's content
 publicReadAccess: true,
 blockPublicAccess: new s3.BlockPublicAccess({
 blockPublicAcls: false,
 blockPublicPolicy: false,
 ignorePublicAcls: false,
 restrictPublicBuckets: false,
 }),
 // Delete everything in bucket when this CF Stack is deleted!
 autoDeleteObjects: true,
 removalPolicy: cdk.RemovalPolicy.DESTROY,
});

// Print URL after deploy is done
new cdk.CfnOutput(this, 'WebsiteURL', {
 value: bucket.bucketWebsiteUrl,
});

Deploy:

aws login
cdk bootstrap
cdk deploy

I had to trouble shoot some things:

  1. Upgrade aws cli to use aws login
  2. Run aws sts get-caller-identity to check my account info
  3. Run vi ~/.aws/config and fix my region string
[default]
login_session = arn:aws:iam::...
// I put some random letters here by mistake in CLI
region = us-east-1

Next Steps

Now that I've figured out how to deploy with cdk, I'm going to write out my whole aws resume infrastructure one service at a time. Not sure yet how to integrate my CI/CD, if I should still use GitHub Actions. And because I'm new to IaC, I don't know how updating the infrastructure works. I know, for example, I can add existing buckets to my stack.js file. I would need to learn how to prevent configuration drift. Luckily there's a mod for that – Solutions Architect Mod: Blueprint Drift.