0

When using

sam build && sam deploy --guided --capabilities CAPABILITY_IAM

to deploy my image compression lambda I receive the following output:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus                                                                                           ResourceType                                                                                             LogicalResourceId                                                                                        ResourceStatusReason                                                                                   
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS                                                                                       AWS::IAM::Role                                                                                           ImageCompressionLambdaRole                                                                               Resource creation Initiated                                                                            
CREATE_IN_PROGRESS                                                                                       AWS::IAM::Role                                                                                           ImageCompressionLambdaRole                                                                               -                                                                                                      
CREATE_IN_PROGRESS                                                                                       AWS::S3::Bucket                                                                                          CompressedBucket                                                                                         -                                                                                                      
CREATE_FAILED                                                                                            AWS::IAM::Role                                                                                           ImageCompressionLambdaRole                                                                               Resource creation cancelled                                                                            
CREATE_FAILED                                                                                            AWS::S3::Bucket                                                                                          CompressedBucket                                                                                         regiolix-dev-compressed already exists                                                                 
ROLLBACK_IN_PROGRESS                                                                                     AWS::CloudFormation::Stack                                                                               regiolix-img-compression                                                                                 The following resource(s) failed to create: [ImageCompressionLambdaRole, CompressedBucket]. Rollback   
                                                                                                                                                                                                                                                                                                                           requested by user.                                                                                     
DELETE_IN_PROGRESS                                                                                       AWS::IAM::Role                                                                                           ImageCompressionLambdaRole                                                                               -                                                                                                      
DELETE_COMPLETE                                                                                          AWS::S3::Bucket                                                                                          CompressedBucket                                                                                         -                                                                                                      
DELETE_COMPLETE                                                                                          AWS::IAM::Role                                                                                           ImageCompressionLambdaRole                                                                               -                                                                                                      
ROLLBACK_COMPLETE                                                                                        AWS::CloudFormation::Stack                                                                               regiolix-img-compression                                                                                 -                                                                                                      
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

template.yml

# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html

# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  image-compression

# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31

Parameters:
  UncompressedBucketName:
    Type: String
    Description: "Input Bucket / Source Bucket"
  CompressedBucketName:
    Type: String
    Description: "Output Bucket / Destination Bucket"

# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
  UncompressedBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref UncompressedBucketName
  CompressedBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref CompressedBucketName
  # Each Lambda function is defined by properties:
  # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction

  # This is a Lambda function config associated with the source code: hello-from-lambda.js
  ImageCompressionLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/index.handler
      Runtime: nodejs12.x
      MemorySize: 128
      Timeout: 100
      Environment:
        Variables:
          UNCOMPRESSED_BUCKET: !Ref UncompressedBucketName
          COMPRESSED_BUCKET: !Ref CompressedBucketName
      Policies:
        - S3ReadPolicy:
            BucketName: !Ref UncompressedBucketName
        - S3WritePolicy:
            BucketName: !Ref CompressedBucketName
      Events:
        CompressImageEvent:
          Type: S3
          Properties:
            Bucket: !Ref UncompressedBucket
            Events: s3:ObjectCreated:*

I do not know what I should do to make this work. Both buckets already exists. When I remove them from the Resource entry in the template.yml the deployment fails with an error telling me to reference a bucket in Resources for the provided events.

Any ideas on how to solve this issue?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Markus G.
  • 1,620
  • 2
  • 25
  • 49
  • You need to `import` them into the stack or you need to modify the template in a way that you only *reference* the buckets, e.g. by name but do not attempt to create it. – luk2302 Jul 16 '21 at 09:32
  • how do I mark a resource to be imported? – Markus G. Jul 16 '21 at 09:40
  • You do not *mark* them, you need to actually import them: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import.html - ***very*** painful because CloudFormation. – luk2302 Jul 16 '21 at 09:44
  • @luk2302 CF supports that, but does SAM? – jordanm Jul 16 '21 at 14:07

0 Answers0