Popular as it is, Infrastructure as Code also known as IaC, is a practice and process of managing and deploying infrastructure through code. Now, we got the flexibility to choose from variety of IaC and SDK tools for infrastructure cloud provisioning. Some of these tools are native and part of the managed cloud services, and the rest of them are mostly free and open-source software (FOSS). Today’s topic is AWS, so let’s go through each infrastructure deployment method.
AWS CloudFormation
AWS CloudFormation is a managed IaC service that helps us provision AWS resources using declarative language. Here’s the difference between Declarative vs Imperative IaC though.
Writing CF templates comes in two flavors, JSON and YAML. I prefer JSON over YAML any day.
Deploying S3 bucket in JSON should look like:
{
"Resources": {
"S3Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "devcoops-bucket"
}
}
}
}
AWS CDK
AWS CDK or known as Cloud Development Kit is yet another native tool by AWS, which is basically an abstraction on top of CloudFormation. Behind the curtains, the CDN scripts are transformed to a CloudFormation definitions. Also, it’s better than CF in a way that allows us to define and deploy infrastructure in an imperative way, by using one or more of the familiar programming languages, including JavaScript, TypeScript, Python, C# and Java. All of these languages come with much better features, high-level abstractions and reusability than JSON or YAML.
Example of spinning up S3 bucket using Python and boto3:
import boto3
s3_client = boto3.client('s3')
s3.create_bucket(Bucket=devcoops-bucket)
AWS CLI
AWS CLI is a command-line interface SDK product and it’s the laziest way to create resources in AWS. It’s definitely not recommended for managing and deploying infrastructure, since there is no state management, like CloudFormation and CDK for example.
Create a S3 bucket:
aws s3 mb s3://devcoops-bucket
Third-party tools
Speaking of third-party IaC tools, Terraform being the industry standard, comes to mind first. Pulumi, on the other hand, is a promising contender.
Deploy S3 bucket using Terraform:
resource "aws_s3_bucket" "devcoops" {
bucket = "devcoops-example-bucket"
acl = "private"
tags = {
Name = "devcoops"
Environment = "Prod"
}
}
Management Console
Last but not least, there is always the good ol console GUI, making things simple, if managing a single resource after all.
Conclusion
So, which one is the best fit?! The comparison always comes between CloudFormation and Terraform. Speaking from my personal experience, Terraform is the best fit as an IaC tool standard. On the other hand, Pulumi looks promising, and if you are fully invested in AWS, the CDK is good on paper, although I haven’t read and heard any stories about it as much as for CF. But, if you have any to share, please let me know.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.