Skip to content

Instantly share code, notes, and snippets.

@thomasmichaelwallace
Last active September 8, 2022 12:55
Show Gist options
  • Select an option

  • Save thomasmichaelwallace/82c55855d16a265f20dc0a5df4653e82 to your computer and use it in GitHub Desktop.

Select an option

Save thomasmichaelwallace/82c55855d16a265f20dc0a5df4653e82 to your computer and use it in GitHub Desktop.

Revisions

  1. thomasmichaelwallace revised this gist Jun 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lambda_function.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ def lambda_to_ec2(event, context):
    service httpd start
    chkconfig httpd on
    echo """ + message + """ > /var/www/html/index.html
    shutdown -H +5"""
    shutdown -h +5"""

    print 'Running script:'
    print init_script
  2. thomasmichaelwallace created this gist Aug 9, 2017.
    45 changes: 45 additions & 0 deletions lambda_function.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    """ Lambda to launch ec2-instances """
    import boto3

    REGION = 'eu-central-1' # region to launch instance.
    AMI = 'ami-24bd1b4b'
    # matching region/setup amazon linux ami, as per:
    # https://aws.amazon.com/amazon-linux-ami/
    INSTANCE_TYPE = 'm3.medium' # instance type to launch.

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

    def lambda_to_ec2(event, context):
    """ Lambda handler taking [message] and creating a httpd instance with an echo. """
    message = event['message']

    # bash script to run:
    # - update and install httpd (a webserver)
    # - start the webserver
    # - create a webpage with the provided message.
    # - set to shutdown the instance in 5 minutes.
    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"""

    print 'Running script:'
    print init_script

    instance = EC2.run_instances(
    ImageId=AMI,
    InstanceType=INSTANCE_TYPE,
    MinCount=1, # required by boto, even though it's kinda obvious.
    MaxCount=1,
    InstanceInitiatedShutdownBehavior='terminate', # make shutdown in script terminate ec2
    UserData=init_script # file to run on instance init.
    )

    print "New instance created."
    instance_id = instance['Instances'][0]['InstanceId']
    print instance_id

    return instance_id