0% found this document useful (0 votes)
11 views14 pages

Lab 15

This document provides instructions for using AWS Lambda to create an EC2 instance. It imports the necessary AWS and OS modules, defines environment variables for the AMI, instance type, key name, subnet ID and region. The lambda function takes event data, starts an EC2 instance using boto3 with the defined parameters, runs user data to install and configure Apache httpd, returns the instance ID and prints it. It concludes by describing steps to test the function by creating a key pair, choosing a subnet in VPC and configuring security group inbound rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Lab 15

This document provides instructions for using AWS Lambda to create an EC2 instance. It imports the necessary AWS and OS modules, defines environment variables for the AMI, instance type, key name, subnet ID and region. The lambda function takes event data, starts an EC2 instance using boto3 with the defined parameters, runs user data to install and configure Apache httpd, returns the instance ID and prints it. It concludes by describing steps to test the function by creating a key pair, choosing a subnet in VPC and configuring security group inbound rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab 15: demo on AWS Lambda Function to create EC2 instance.

import os

import boto3

AMI = os.environ['AMI']

INSTANCE_TYPE = os.environ['INSTANCE_TYPE']

KEY_NAME = os.environ['KEY_NAME']

SUBNET_ID = os.environ['SUBNET_ID']

REGION = os.environ['REGION']

ec2 = boto3.client('ec2', region_name=REGION)

def lambda_handler(event, context):

message = event['message']

init_script = """#!/bin/bash

yum update -y

yum install -y httpd24

service httpd start

chkconfig httpd on

echo """ + message + """ > /var/www/html/index.html


shutdown -h +5"""

instance = ec2.run_instances(

ImageId=AMI,

InstanceType=INSTANCE_TYPE,

KeyName=KEY_NAME,

SubnetId=SUBNET_ID,

MaxCount=1,

MinCount=1,

InstanceInitiatedShutdownBehavior='terminate',

UserData=init_script

instance_id = instance['Instances'][0]['InstanceId']

print(instance_id)

return instance_id

Then go to test key


Create a dupilacate tab and open ec2

To write the key pair name


Open vpc and choose a rando sub net value

Click on save
Click the security goup

And go to inbound

You might also like