0

I am trying to create EC2 instance via Lambda using Boto3. Creation of EC2 works ok but the passing of variables into USERDATA script.

I've tried different following ways of calling in USERDATA : os.environ['VARIABLE'], VARIABLE, $VARIABLE, ${VARIABLE}

import os
import boto3

EC2             = boto3.client('ec2', region_name=os.environ['REGION'])
AMI             = os.environ['AMI']
INSTANCE_TYPE   = os.environ['INSTANCE_TYPE']

def lambda_to_ec2(event, context):
    init_script = """#!/bin/bash
    yum update -y   
    echo VARIABLE
    shutdown -h +5"""

    print 'Running script:'
    print init_script

    instance = EC2.run_instances(
        ImageId= AMI,
        InstanceType= INSTANCE_TYPE,
        MinCount=1,
        MaxCount=1,
        InstanceInitiatedShutdownBehavior='terminate',
        UserData=init_script
    )

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

    return instance_id

I`d like to be able to use environment variables in init_script which is passed to UserData.

aRagornol
  • 235
  • 3
  • 17

1 Answers1

1

Found out this is really python issue. When using tripple quotes you can access the variables following way In Python, can you have variables within triple quotes? If so, how?

aRagornol
  • 235
  • 3
  • 17