deployment/ElbHostnameAsTargetUsingLambdaExistingInfra.yaml [157:287]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:GetObject
                  - s3:DeleteObject
                  - s3:CreateBucket
                  - s3:DeleteBucket
                Resource: !Join
                  - ""
                  - - 'arn:aws:s3:::'
                    - !Ref DstS3BucketName
                    - '/*'                        
              - Effect: Allow
                Action:
                  - s3:ListAllMyBuckets
                  - cloudwatch:PutMetricData
                  - elasticloadbalancing:RegisterTargets
                  - elasticloadbalancing:DeregisterTargets
                  - elasticloadbalancing:DescribeTargetHealth
                  - ec2:CreateNetworkInterface
                  - ec2:DescribeNetworkInterfaces
                  - ec2:DeleteNetworkInterface                       
                Resource: "*"
  
  S3ObjectLambda:
    Type: AWS::Lambda::Function
    Properties:
      Handler: 'index.handler'
      Role: !GetAtt
        - LambdaExecutionRole
        - Arn
      Code:
        ZipFile: |
          import json
          import logging
          import os
          import sys
          import urllib.request
          from urllib.parse import urlparse

          import boto3
          import cfnresponse
          from botocore.exceptions import ClientError

          try:
              s3_resource = boto3.resource('s3')
              s3_client = boto3.client('s3')
          except ClientError as e:
              logger.error(f"ERROR: failed to connect to S3 resource or client: {e}")
              sys.exit(1)

          def obj_info(url):
              url_path = urlparse(url).path
              obj_name = os.path.basename(url_path)
              obj_path = f'/tmp/{obj_name}'
              return obj_name, obj_path
          
          def upload_to_s3(url, dst_bucket, obj_path, obj_name):
              urllib.request.urlretrieve(url, obj_path)
              s3_resource.Bucket(dst_bucket).upload_file(Filename=obj_path, Key=obj_name)

          def handler(event, context):
              logger = logging.getLogger()
              logger.setLevel(logging.INFO)
              logger.info("INFO: Received event: {}".format(json.dumps(event)))

              responseData = {}
              responseStatus = cfnresponse.FAILED

              print(event["ResourceProperties"])
              try:
                  src_url = event["ResourceProperties"]["SourceUrl"]
                  dst_s3 = event["ResourceProperties"]["DstS3Bucket"]
                  aws_region = event["ResourceProperties"]["AwsRegion"]
              except Exception as e:
                  logger.error(f"parameter retival failure: {e}")
                  sys.exit(1)
              
              if isinstance(dst_s3, list):
                  dst_s3 = dst_s3[0]

              obj_name = obj_info(src_url)[0]
              obj_path = obj_info(src_url)[1]
              try:
                  if event["RequestType"] == "Delete":    
                      s3_resource.Object(dst_s3, obj_name).delete()
                      responseStatus = cfnresponse.SUCCESS
                      cfnresponse.send(event, context, responseStatus, responseData)
              except Exception:
                  logger.exception("Signaling failure to CloudFormation.")
                  cfnresponse.send(event, context, cfnresponse.FAILED, {})
              if event["RequestType"] == "Create":
                  logger.info(f"INFO: Copying {obj_name} to {dst_s3}")
                  upload_to_s3(src_url, dst_s3, obj_path, obj_name)
                  responseStatus = cfnresponse.SUCCESS
                  cfnresponse.send(event, context, responseStatus, responseData)

      Runtime: python3.7
      Timeout: 45

  S3EditObject:
    Type: Custom::S3EditObject
    Properties:
      ServiceToken: !GetAtt S3ObjectLambda.Arn
      SourceUrl: https://github.com/aws-samples/hostname-as-target-for-elastic-load-balancer/blob/main/source/ElbHostnameAsTarget.zip?raw=true
      DstS3Bucket: !If [CreateDstS3Bucket, !Ref S3BucketForLambda, !Ref DstS3BucketName]
      AwsRegion: !Ref AWS::Region
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



deployment/ElbHostnameAsTargetUsingLambdaNewInfra.yaml [566:696]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:GetObject
                  - s3:DeleteObject
                  - s3:CreateBucket
                  - s3:DeleteBucket
                Resource: !Join
                  - ""
                  - - 'arn:aws:s3:::'
                    - !Ref DstS3BucketName
                    - '/*'            
              - Effect: Allow
                Action:
                  - s3:ListAllMyBuckets
                  - cloudwatch:PutMetricData
                  - elasticloadbalancing:RegisterTargets
                  - elasticloadbalancing:DeregisterTargets
                  - elasticloadbalancing:DescribeTargetHealth
                  - ec2:CreateNetworkInterface
                  - ec2:DescribeNetworkInterfaces
                  - ec2:DeleteNetworkInterface                       
                Resource: "*"

  S3ObjectLambda:
    Type: AWS::Lambda::Function
    Properties:
      Handler: 'index.handler'
      Role: !GetAtt
        - LambdaExecutionRole
        - Arn
      Code:
        ZipFile: |
          import json
          import logging
          import os
          import sys
          import urllib.request
          from urllib.parse import urlparse

          import boto3
          import cfnresponse
          from botocore.exceptions import ClientError

          try:
              s3_resource = boto3.resource('s3')
              s3_client = boto3.client('s3')
          except ClientError as e:
              logger.error(f"ERROR: failed to connect to S3 resource or client: {e}")
              sys.exit(1)

          def obj_info(url):
              url_path = urlparse(url).path
              obj_name = os.path.basename(url_path)
              obj_path = f'/tmp/{obj_name}'
              return obj_name, obj_path
          
          def upload_to_s3(url, dst_bucket, obj_path, obj_name):
              urllib.request.urlretrieve(url, obj_path)
              s3_resource.Bucket(dst_bucket).upload_file(Filename=obj_path, Key=obj_name)

          def handler(event, context):
              logger = logging.getLogger()
              logger.setLevel(logging.INFO)
              logger.info("INFO: Received event: {}".format(json.dumps(event)))

              responseData = {}
              responseStatus = cfnresponse.FAILED

              print(event["ResourceProperties"])
              try:
                  src_url = event["ResourceProperties"]["SourceUrl"]
                  dst_s3 = event["ResourceProperties"]["DstS3Bucket"]
                  aws_region = event["ResourceProperties"]["AwsRegion"]
              except Exception as e:
                  logger.error(f"parameter retival failure: {e}")
                  sys.exit(1)
              
              if isinstance(dst_s3, list):
                  dst_s3 = dst_s3[0]

              obj_name = obj_info(src_url)[0]
              obj_path = obj_info(src_url)[1]
              try:
                  if event["RequestType"] == "Delete":    
                      s3_resource.Object(dst_s3, obj_name).delete()
                      responseStatus = cfnresponse.SUCCESS
                      cfnresponse.send(event, context, responseStatus, responseData)
              except Exception:
                  logger.exception("Signaling failure to CloudFormation.")
                  cfnresponse.send(event, context, cfnresponse.FAILED, {})
              if event["RequestType"] == "Create":
                  logger.info(f"INFO: Copying {obj_name} to {dst_s3}")
                  upload_to_s3(src_url, dst_s3, obj_path, obj_name)
                  responseStatus = cfnresponse.SUCCESS
                  cfnresponse.send(event, context, responseStatus, responseData)

      Runtime: python3.7
      Timeout: 45

  S3EditObject:
    Type: Custom::S3EditObject
    Properties:
      ServiceToken: !GetAtt S3ObjectLambda.Arn
      SourceUrl: https://github.com/aws-samples/hostname-as-target-for-elastic-load-balancer/blob/main/source/ElbHostnameAsTarget.zip?raw=true
      DstS3Bucket: !If [CreateDstS3Bucket, !Ref S3BucketForLambda, !Ref DstS3BucketName]
      AwsRegion: !Ref AWS::Region
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



