How infrastructure code is executed in AWS?

In AWS:

  • The code for infrastructure will be in a simple JSON format.
  • This JSON code will be organized into files called templates.
  • These templates can be deployed on AWS DevOps and then managed as stacks.
  • Later the CloudFormation service will do the Creating, deleting, updating, etc. operation in the stack.

In AWS, infrastructure code is typically executed using Infrastructure as Code (IaC) tools. The correct answer will depend on the specific tools and methodologies employed in a DevOps environment. However, a common approach involves using tools like AWS CloudFormation, AWS CDK (Cloud Development Kit), or third-party tools like Terraform.

AWS CloudFormation: AWS CloudFormation is a native AWS service that allows you to define and provision AWS infrastructure as code. You create templates written in JSON or YAML, describing the resources and their configurations, and then deploy those templates to AWS.

Example CloudFormation template (YAML):

yaml

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0c55b159cbfafe1f0
InstanceType: t2.micro
AWS CDK (Cloud Development Kit): AWS CDK is a software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. It allows you to define infrastructure using familiar programming languages like Python, TypeScript, or Java.

Example AWS CDK code (TypeScript):

typescript

import * as cdk from ‘aws-cdk-lib’;
import * as ec2 from ‘aws-cdk-lib/aws-ec2’;

const app = new cdk.App();
const stack = new cdk.Stack(app, ‘MyStack’);

new ec2.Instance(stack, ‘MyInstance’, {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
});
Terraform: Terraform is a third-party IaC tool that is not specific to AWS but supports multiple cloud providers. It uses its own domain-specific language (HCL) to describe infrastructure and can be used to manage AWS resources.

Example Terraform configuration:

hcl

resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = “t2.micro”
}
These tools allow DevOps teams to manage and version control infrastructure configurations, making it easier to provision, update, and maintain AWS resources in a consistent and repeatable manner.