@@ -0,0 +1,2728 @@
+'''%0ACopyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A%0ALicensed under the Apache License, Version 2.0 (the %22License%22). You may not use this file%0Aexcept in compliance with the License. A copy of the License is located at%0A%0A    http://aws.amazon.com/apache2.0/%0A%0Aor in the %22license%22 file accompanying this file. This file is distributed on an %22AS IS%22%0ABASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the%0ALicense for the specific language governing permissions and limitations under the License.%0A'''%0A%0Afrom __future__ import print_function%0A%0Aprint('Loading function')%0A%0Aimport json, boto3, re%0A%0Adef lambda_handler(event, context):%0A    # print(%22Received event: %5Cn%22 + json.dumps(event))%0A%0A    # If CreateTags failed nothing to do%0A    if 'errorCode' in event%5B'detail'%5D:%0A        print('CreateTags failed with error code %7B%7D and error message %22%7B%7D%22, nothing to do.'%0A            .format(event%5B'detail'%5D%5B'errorCode'%5D, event%5B'detail'%5D%5B'errorMessage'%5D))%0A        return%0A%0A    region = event%5B'detail'%5D%5B'awsRegion'%5D%0A    ec2 = boto3.client('ec2', region_name=region)%0A%0A    instance_ids = %5B%5D%0A    is_instance = re.compile('i-%5B0-9a-f%5D+')%0A    # Run instances may create several instances, then the event will contain %0A    # several instances%0A    for item in event%5B'detail'%5D%5B'requestParameters'%5D%5B'resourcesSet'%5D%5B'items'%5D:%0A        if is_instance.match(item%5B'resourceId'%5D):%0A            instance_ids.append(item%5B'resourceId'%5D)%0A    %0A    # check if we were tagging any instances%0A    if len(instance_ids) == 0:%0A        return%0A%0A    tags = %5B%5D%0A    for tag in event%5B'detail'%5D%5B'requestParameters'%5D%5B'tagSet'%5D%5B'items'%5D:%0A        tags.append(%7B%0A            'Key': tag%5B'key'%5D,%0A            'Value': tag%5B'value'%5D%0A        %7D)%0A%0A    # If the number of created instances then describe instances may be paginated%0A    paginator = ec2.get_paginator('describe_instances')%0A    instances_iterator = paginator.paginate(%0A        DryRun=False,%0A        InstanceIds=instance_ids%0A    )%0A%0A    for page in instances_iterator:%0A        resources = %5B%5D%0A        for reservation in page%5B'Reservations'%5D:%0A            for instance in reservation%5B'Instances'%5D:%0A                for eni in instance%5B'NetworkInterfaces'%5D:%0A                    resources.append(eni%5B'NetworkInterfaceId'%5D)%0A%0A                for volume in instance%5B'BlockDeviceMappings'%5D:%0A                    if 'Ebs' in volume:%0A                        resources.append(volume%5B'Ebs'%5D%5B'VolumeId'%5D)%0A%0A        print(%22Tagging resorces for instance ids:%5Cn%5B%7B%7D%5D%22.format(', '.join(instance_ids)))%0A        print(%22Resources to be tagged:%5Cn%5B%7B%7D%5D%22.format(', '.join(resources)))%0A%0A        ec2.create_tags(%0A            DryRun=False,%0A            Resources=resources,%0A            Tags=tags%0A        )%0A%0A    return%0A

Add tag EC2 dependencies Lambda Python code.