Quick Summary
This blog is a tutorial on implementing Infrastructure as Code with AWS CloudFormation and AWS CDK. It provides a step-by-step guide to automating cloud resource management, including creating S3 buckets, IAM roles, and Lambda functions. The blog further covers how to use CloudFormation templates and AWS CDK to define and deploy resources efficiently, ensuring consistent and reliable infrastructure management.
Infrastructure as Code (IaC) is a method that enables developers to control and manage infrastructure using code, removing the necessity for manual setups. This approach streamlines the creation, configuration, and management of resources such as servers, databases, and networks.
Traditionally, IT teams had to set up servers and databases by hand, which led to various challenges.
IaC addresses these issues by automating infrastructure management, ensuring consistency, visibility, and easier collaboration. By handling infrastructure like software, teams can use good practices such as keeping track of changes, testing their work, and delivering updates regularly.
Tools such as AWS CloudFormation and AWS CDK make it easy for developers to create, modify, and manage infrastructure efficiently, leading to faster and more reliable deployments.
Now, let’s talk about AWS CloudFormation.
AWS CloudFormation is a service designed to help you manage your cloud resources using code. This approach enables you to operate your resources securely and reliably, allowing for efficient deployment and updates in your cloud environment. Instead of manually setting up each AWS resource, you can write a JSON or YAML template specifying everything you need, such as servers, databases, and networking components. CloudFormation automates the creation and configuration of these resources, ensuring consistency and reducing the chances of errors.
AWS CloudFormation provides the following benefits:
Using AWS CloudFormation, you can effectively manage your cloud infrastructure, ensuring it is efficient, reliable, and easily maintainable.
Open a code editor like Visual Studio Code. Create a new file named “example.yaml” and add this code:
# Create a new S3 Bucket Resources: MyS3Bucket: Type: 'AWS::S3::Bucket' Properties: BucketName: example-bac Tags: - Key: Dev Value: Bacancy MyIAMRole: Type: 'AWS::IAM::Role' Properties: RoleName: myRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole MyLambdaFunction: Type: 'AWS::Lambda::Function' Properties: FunctionName: MyFunction Handler: index.handler Role: !GetAtt MyIAMRole.Arn Code: ZipFile: | def handler(event, context): return "Hello, World!" Runtime: python3.8 MyDynamoDBTable: Type: 'AWS::DynamoDB::Table' Properties: TableName: MyTable AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5
This CloudFormation template provisions four AWS resources: an S3 bucket named `example-bac` with a development tag, an IAM role allowing AWS Lambda to assume it, a Python-based Lambda function that returns “Hello, World!” upon invocation, and a DynamoDB table named `MyTable` with a string-based primary key `id`. Each resource is configured with defined properties to handle data storage, function execution, and permission management within the AWS environment.
a. Login into the AWS Console account
b. Search on cloud formation and create on “Create Stack”
c. Click on Choose an existing template
> Choose Upload a template file
> Upload the above example. yaml
d. Enter stack name (e.g. example)
e. Give appropriate tags
(e.g., Key: Name, Value: Cloud formation)
f. Click on Submit
Here you go! (Wait a few seconds and click the refresh button to see the updated list of events.) All the resources are currently being processed.
Click on the “ Resources” tab, and we will see all the resources that have been created.
Here, we can see that an S3 bucket is created.
It will charge running resources. So, we need to terminate the resources.
Here, Click on delete
Newly created resources are not running anymore. This is how we can use Cloudformation as IaC and manage all AWS resources.
For expert guidance on managing your AWS resources efficiently, consider our AWS managed services.
AWS CDK (Cloud Development Kit) is a free tool that helps developers create cloud infrastructure using standard programming languages like Python, JavaScript, and TypeScript. CDK makes it easier to write Infrastructure as Code (IaC) by allowing you to write code that produces CloudFormation templates, which helps you set up and manage AWS resources more efficiently.
Open your terminal and run these commands
npm install -g aws-cdk
mkdir my-cdk-app
cd my-cdk-app
cdk init app
–language typescript
Open the generated lib/my-cdk-app-stack.ts
file.
Define AWS resources in this file, for example, creating an S3 bucket:
import * as s3 from 'aws-cdk-lib/aws-s3'; import { Stack, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs'; // import * as sqs from 'aws-cdk-lib/aws-sqs'; export class MyCdkAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); new s3.Bucket(this, 'MyCDKBucket', { versioned: true, }); } }
This will create one s3 bucket in our AWS Account.
Run this command in the terminal
cdk synth
This command generates a CloudFormation template based on your defined infrastructure.
cdk deploy
This command provisions your defined resources (like S3 buckets) in AWS.
Clean up resources by running: CDK destroy
In conclusion, leveraging Infrastructure as Code with AWS CloudFormation and AWS CDK streamlines managing cloud resources, enhancing efficiency and scalability. By adopting these tools, developers can automate deployments and maintain consistency across environments, leading to more robust and resilient infrastructure. For organizations wanting to improve their cloud strategy, you can opt for our AWS consulting services to build more robust and more reliable infrastructure. This makes processes more accessible, allowing teams to concentrate on innovation and providing more value.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.