23

I have written an AWS Lambda function in Python that filters through instances and turns them on or off depending on how they are tagged. This will show you a working function, and the set-up needed to get it working. If you have questions on anything post it in the comments.

Here is my Lambda Function as of now:

import boto3
def lambda_handler(event, context):
    startStop = event['startStop']
    vertical = event['vertical']
    isRunning = ''

    if(startStop == 'start'):
        isRunning = 'stopped'
    elif (startStop == 'stop'):
        isRunning = 'running'

    ec2 = boto3.resource('ec2')
    filters = [
        {
            'Name': 'instance-state-name', 
            'Values': [isRunning] 
        },
        {
            'Name': 'tag:Vertical',
            'Values': [vertical]
        }
    ]

    instances = ec2.instances.filter(Filters=filters)

    runningInstances = [instance.id for instance in instances]

    if len(runningInstances) > 0:
        if(startStop == 'start'):
            shuttingDown = ec2.instances.filter(InstanceIds=runningInstances).start()
        elif (startStop == 'stop'):
            shuttingDown = ec2.instances.filter(InstanceIds=runningInstances).stop()

For reference, here is my mapping template:

{
    "startStop":"$input.params('startStop')",
    "vertical":"$input.params('vertical')"
}

And this is how I am passing in the parameters within the URL:

https://awslambdaapiurl.../prod/-params?startStop=start&vertical=TEST
4
  • I ended up figuring out the answer to my original question, so I have posted the working code as a resource for anyone to look at. Commented Jul 19, 2017 at 20:31
  • 3
    Can you just paste what you did as an answer and mark this as answered. Commented Jul 21, 2017 at 17:50
  • Sure, should I just delete the post and then re-post it as an answer? @AbhignaNagaraja Commented Jul 21, 2017 at 18:31
  • @DougAndres It's not necessary to repost. Just rollback the changes in the question as it was at the beginning (stackoverflow.com/revisions/45198321/1), and add what you did as an answer for this question. Then, mark it as answered and that's it! :-) Commented Jul 12, 2018 at 22:45

3 Answers 3

5

Pass in parameter values in a mapping template:

{
    "startStop":"$input.params('startStop')",
    "vertical":"$input.params('vertical')"
}

Read parameter values via event Object:

    startStop = event['startStop']
    vertical = event['vertical']
Sign up to request clarification or add additional context in comments.

Comments

4

You can use the queryStringParameters on event object.

request_data = event['queryStringParameters']
startStop = request_data['startStop']
vertical = request_data['vertical']

Comments

4

The Asker answered their question by editing their post, but for other newbies: When passing parameters via URL, the event parameter within the lambda_handler function is a dictionary where the URL parameters are within the queryStringParameters key:

Entering in your browser https://awslambdaapiurl/?startStop=start&vertical=TEST would be accessed by python within the lambda_handler function as:

event['queryStringParameters']['startStop']
event['queryStringParameters']['vertical']

1 Comment

My answer is identical to stackoverflow.com/a/64043328/4420657, just more wordy. I am leaving my answer in addition since I feel the more verbose explanation was needed for my brain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.