Automating AWS EC2 Instance Lifecycle with Lambda and CloudFormation: A Step-by-Step Guide

One of the most powerful features of Amazon Web Services (AWS) is the ability to automate operational tasks. In this article, we’ll walk you through creating a system that spins up an AWS EC2 T2 instance at 10:00 PM EST every day, executes a Python script stored in an S3 bucket, and then terminates the instance by 10:30 PM EST. This will be accomplished using AWS Lambda functions and AWS CloudFormation, providing a powerful, automated, and time-controlled solution.

Prerequisites:

  • An AWS account.
  • Sufficient permissions to create and manage EC2 instances, S3 buckets, AWS Lambda functions, IAM roles, and CloudFormation stacks.
  • Basic knowledge of Python, AWS services, and YAML (for CloudFormation).

Steps:

1. Creating an IAM Role for Lambda:

First, we need to create an IAM role that AWS Lambda will assume to perform operations. This role needs the following permissions:

  • AmazonEC2FullAccess: To manage EC2 instances.
  • AmazonS3ReadOnlyAccess: To read files from S3.
  • AWSLambdaBasicExecutionRole: To write logs to CloudWatch.

These roles can be added through the AWS Management Console, under IAM service, Roles section.

2. Writing the Lambda Function:

The Lambda function will be responsible for starting the EC2 instance, triggering the script execution, and terminating the EC2 instance. The script would be as follows:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.resource('ec2')
    
    # Replace with your instance ID
    instance_id = 'your-ec2-instance-id'
    instance = ec2.Instance(instance_id)

    if event.get('action') == 'start':
        # Start the instance
        instance.start()
        instance.wait_until_running()
        # Execute the script
        # Depending on how you setup your instance, you might need to SSH into the instance and execute your script manually
        # Or, you can use User Data or Instance Metadata to automatically run the script upon instance startup
    elif event.get('action') == 'stop':
        # Terminate the instance
        instance.terminate()
    else:
        print('Invalid event')

3. CloudFormation Template:

We create two CloudWatch Events rules, one to trigger the function with a “start” event at 10:00 PM EST, and another with a “stop” event at 10:30 PM EST.

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  LambdaExecutionRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: 'lambda.amazonaws.com'
            Action: 'sts:AssumeRole'
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/AmazonEC2FullAccess'
        - 'arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess'
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'

  MyLambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code: 
        S3Bucket: 'freshers-in'
        S3Key: 'code-bkt/viwershipt/daily-viewers-action.py'
      Handler: 'daily-viewers-action.lambda_handler'
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: 'python3.9'

  StartSchedule:
    Type: 'AWS::Events::Rule'
    Properties:
      Description: 'Start my EC2 instance at 10:00 PM EST daily'
      ScheduleExpression: 'cron(00 22 * * ? *)'
      Targets:
        - Arn: !GetAtt MyLambdaFunction.Arn
          Id: 'TargetFunctionV1'
          Input: '{"action": "start"}'

  StopSchedule:
    Type: 'AWS::Events::Rule'
    Properties:
      Description: 'Stop my EC2 instance at 10:30 PM EST daily'
      ScheduleExpression: 'cron(30 22 * * ? *)'
      Targets:
        - Arn: !GetAtt MyLambdaFunction.Arn
          Id: 'TargetFunctionV2'
          Input: '{"action": "stop"}'

4. Deploying the Stack:

Now you’re ready to deploy the stack. Navigate to the CloudFormation console and create a new stack. Select “Upload a template file”, upload your YAML file, and follow the wizard to deploy your stack.

That’s it! Now, your EC2 instance will start at 10:00 PM EST daily, execute the Python script, and terminate by 10:30 PM EST.

Refer more on python here :

Author: user

Leave a Reply