# AWS Day 48: Automating Infrastructure Deployment with AWS CloudFormation

Continuing my **KodeKloud 100 Days of Cloud** journey, Day 48 focused on using **AWS CloudFormation to automate Lambda deployment**.

Instead of creating the Lambda function and IAM role manually from the AWS Console, I defined the infrastructure as code in a CloudFormation template. This makes the deployment repeatable, consistent, and easier to manage.

## What I Built

For this lab, I created:

*   CloudFormation stack: `xfusion-lambda-app`
    
*   Lambda function: `xfusion-lambda`
    
*   IAM role: `lambda_execution_role`
    
*   Runtime: Python 3.12
    
*   Lambda response status code: `200`
    
*   Response body: `Welcome to KKE AWS Labs!`
    

## Step 1: Create the CloudFormation Template

I created the template on the AWS client host:

```plaintext
/root/xfusion-lambda.yml
```

The template defines both the IAM role and Lambda function, so CloudFormation can create the required resources automatically.

One important part is the Lambda execution role:

```plaintext
RoleName: lambda_execution_role
```

The role uses the AWS managed policy:

```plaintext
AWSLambdaBasicExecutionRole
```

which allows Lambda to write its execution logs to CloudWatch.

![](https://cdn.hashnode.com/uploads/covers/6683e9700c2137ac86599d6e/379d79d5-208b-4b0e-82f1-db2d37e9f181.png align="center")

## Step 2: Configure the Lambda Function

The Lambda function was configured with:

```plaintext
FunctionName: xfusion-lambda
Runtime: python3.12
Handler: index.lambda_handler
```

The function returns a successful HTTP-style response:

```plaintext
return {
    "statusCode": 200,
    "body": "Welcome to KKE AWS Labs!"
}
```

This satisfies the requirement for both the **200 status code** and the expected response body.

![](https://cdn.hashnode.com/uploads/covers/6683e9700c2137ac86599d6e/24d12e7b-bc82-4e90-9f57-f4f9d22781c3.png align="center")

## Step 3: Deploy the CloudFormation Stack

After creating the YAML template, I deployed it using AWS CLI:

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

The CAPABILITY\_NAMED\_IAM option is required because the template creates a named IAM role.

CloudFormation then handled the creation of the IAM role and Lambda function.

## Step 4: Verify the Lambda Function

After the stack was created successfully, I invoked the Lambda function:

```plaintext
aws lambda invoke \
  --function-name xfusion-lambda \
  --payload '{}' \
  response.json
```

The invocation returned:

```plaintext
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
```

This confirmed that the Lambda function was successfully deployed and executed.

## What I Learned

The main takeaway from this lab was how **AWS CloudFormation turns infrastructure into code**.

## Conclusion

AWS Day 48 was a practical introduction to **Infrastructure as Code with AWS CloudFormation**.

Creating the Lambda function through CloudFormation helped me understand how AWS resources can be defined, deployed, and managed without relying entirely on the AWS Console.

Another step completed in my **AWS, Cloud, and DevOps learning journey**.
