Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PrashantBhatasana/cc4b9ba9b488d892fe1f197f9078fbbd to your computer and use it in GitHub Desktop.
Save PrashantBhatasana/cc4b9ba9b488d892fe1f197f9078fbbd to your computer and use it in GitHub Desktop.
This is lambda function with python language that create ec2 instence.
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
@EmmanuelOuzan
Copy link

EmmanuelOuzan commented May 26, 2021

Hi !
When I test it, it says that the print is missing parentheses... Maybe that's a thing to add ! :)
(I guess your code was written originally for python 2.X

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment