# Day 48: Automating Infrastructure Deployment with AWS CloudFormation

Welcome to Day 48. Today we will learn how to automating Infrastructure depoloyment with AWS CloudFormation.

### What is AWS CloudFormation?

AWS CloudFormation is an **Infrastructure as Code (IaC)** service that allows you to model, provision, and manage your AWS resources by writing a declarative template.

Instead of manually clicking through the AWS Console to create servers, databases, or networking components, you define your entire infrastructure in a **JSON** or **YAML** file. CloudFormation then acts as the "architect," reading your plan and building the resources in the correct order.

### Core Concepts

* **Template:** A text file (YAML or JSON) that serves as the blueprint for your infrastructure. It defines what resources you want (e.g., an EC2 instance or an S3 bucket) and their configurations.
    
* **Stack:** When CloudFormation processes a template, it creates a "Stack." This is a single unit of managed resources. If you delete the stack, all resources defined in the template are deleted together.
    
* **Change Sets:** Before updating a live environment, you can generate a Change Set. This allows you to see a preview of how proposed changes will impact your running resources before you apply them.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039335840/21b2fe41-5b6d-4dd5-936f-e4c4897148d3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039351397/7a3811b8-3fd2-4232-9557-bd64413cc3a1.png align="center")

```bash
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to create a Lambda function and IAM role.

Resources:
  # IAM Role for Lambda Execution
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: lambda_execution_role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  # Lambda Function
  XfusionLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: xfusion-lambda
      Handler: index.lambda_handler
      Runtime: python3.9
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        ZipFile: |
          import json

          def lambda_handler(event, context):
              message = "Welcome to KKE AWS Labs!"
              print(message)
              return {
                  'statusCode': 200,
                  'body': json.dumps(message)
              }

Outputs:
  LambdaArn:
    Description: ARN of the created Lambda function.
    Value: !GetAtt XfusionLambdaFunction.Arn
```

```bash
aws cloudformation validate-template --template-body file:///root/xfusion-lambda.yml
```

```bash
aws cloudformation create-stack --stack-name xfusion-lambda-app --template-body file:///root/xfusion-lambda.yml --capabilities CAPABILITY_NAMED_IAM
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039527032/18426c50-2376-4a72-b9ea-39fba561fd8f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039561502/94d4d613-7dc8-49a5-8cb9-b9cdcf5c20a1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039596292/cbfc416f-c4b9-40a7-9089-9d408462edbf.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039669111/2293fc03-7677-45fb-b33c-7840153bc27e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039642435/92ad42f5-83c2-47da-b401-4d789011180c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039699002/6b1a864c-e6e5-4973-9c8c-31900ca75640.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768039717003/74ae51c5-dba9-47bd-93f7-692483876fb3.png align="center")

Congratulations you did it. It looks good. This lab was successfully completed without any errors. See you in day 49. If you have any issues please let me know I will be happy to assist you. Stay tuned and learn together. If you find my article useful, please kindly like and share it.
