0
votes

I am writing a lambda function to get instance id from cloudwatch of a newly launched aws instance and check for group of tag and if tag is not there, publish to a sns topic, but i am getting below error

Syntax error in module 'lambda_function': unexpected indent (lambda_function.py, line 5)

Here is my updated lambda function

import boto3
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:xxxxx:xxxx'
CODES = ['ttl', 'GGG', 'BRB', 'YLO']
def lambda_handler(event, context):
user = record['userIdentity']['userName']
        region = record['awsRegion']
        ec2 = boto3.resource('ec2', region_name = 'ap-south-1')
    # Extract Instance ID from event
    instance_id = event['detail']['instance-id']
    instance_object = ec2.Instance(instance['instanceId'])
    try:
        tags = {}
        for tag in instance_object.tags:
            tags[tag['Key']] = tag['Value']
        if('Code' not in tags or tags['Code'] not in CODES):
            sns.publish(TopicArn=SNS_TOPIC_ARN, Message=report(instance, user, region))
    except Exception as e:
        print(e)
        raise e
1
Thanks i will look into this and update you.amit singh

1 Answers

0
votes

Looks like you're missing a try statement:

import boto3
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:xxxxx:xxxx'
CODES = ['ttl', 'GGG', 'BRB', 'YLO']
def lambda_handler(event, context):

    # Extract Instance ID from event
    instance_id = event['detail']['instance-id']
    instance_object = ec2.Instance(instance['instanceId'])
    try:
        tags = {}
        for tag in instance_object.tags:
            tags[tag['Key']] = tag['Value']
        if('Code' not in tags or tags['Code'] not in CODES):
            sns.publish(TopicArn=SNS_TOPIC_ARN, Message=report(instance, user, region))
    except Exception as e:
        print(e)
        raise e