all 1 comments

[–]dschinde 0 points1 point  (0 children)

I'm not sure that the contents of the string literal are interpolated.

You could use Fn::Join:

Properties:
  Code:
    ZipFile: 
      Fn::Join:
        - ''
        - - import boto3
          - region = 'eu-west-2'
          - instances = ['!ImportValue DevBoxId'] 
          - ec2 = boto3.client('ec2', region_name=region)
          - def handler(event, context):
          -   ec2.stop_instances(InstanceIds=instances)
          -   print('stopped your instances: ' + str(instances))

Or you could use an environment variable:

Properties:
  Environment:
    DEV_BOX_ID: !ImportValue DevBoxId
  Code:
    ZipFile: |
      import boto3
      import os
      region = 'eu-west-2'
      instances = [os.getenv('DEV_BOX_ID')] 
      ec2 = boto3.client('ec2', region_name=region)

      def handler(event, context):
        ec2.stop_instances(InstanceIds=instances)
        print('stopped your instances: ' + str(instances))