Deployment Failure of SAM Code for S3 and Lambda Using Cross-Stack References

0

I wrote SAM code to create S3 and Lambda resources, but the deployment failed. Could you help me identify what went wrong? Error Message:

AWS::Serverless-2016-10-31 is null Project Structure:

s3Lambda/
│-- functions
│   └-- app.py
│-- templates/
│   │-- root-template.yaml
│   │-- s3.yaml
│   └-- lambda.yaml
└-- parameters/
    └-- samconfig.yaml

File Details: templates/root-template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  BucketName:
    Type: String
    Description: "The name of the S3 Bucket"
  FunctionName:
    Type: String
    Description: "The name of the Lambda function"
  FunctionTimeout:
    Type: Number
    Description: "The maximum time in seconds that the function can run before it is stopped"
    Default: 3
  FunctionMemorySize:
    Type: Number
    Description: "The size of the memory in MB allocated per invocation of the function"

Resources:
  S3Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./s3.yaml
      Parameters:
        BucketName: !Ref BucketName

  LambdaStack:
    Type: AWS::CloudFormation::Stack
    DependsOn:
      - S3Stack
    Properties:
      TemplateURL: ./lambda.yaml
      Parameters:
        FunctionName: !Ref FunctionName
        FunctionTimeout: !Ref FunctionTimeout
        FunctionMemorySize: !Ref FunctionMemorySize

Outputs:
  S3BucketName:
    Description: Name of the S3 bucket
    Value: !GetAtt S3Stack.Outputs.S3BucketName

  S3BucketArn:
    Description: "S3 Bucket ARN"
    Value: !GetAtt S3Stack.Outputs.S3BucketArn

  MyS3LambdaFunctionArn:
    Description: "The ARN of the Lambda Function"
    Value: !GetAtt LambdaStack.Outputs.MyS3LambdaFunctionArn

templates/s3.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Description: s3
Parameters:
  BucketName:
    Type: String
    Description: "The name of the S3 Bucket"

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      Tags:
        - Key: Name
          Value: !Ref BucketName

Outputs:
  S3BucketName:
    Description: Name of the S3 bucket
    Value: !Ref MyS3Bucket
    Export:
      Name: MyS3BucketName
  S3BucketArn:
    Description: "S3 Bucket ARN"
    Value: !GetAtt MyS3Bucket.Arn
    Export:
      Name: MyS3BucketArn

templates/lambda.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Lambda function triggered by S3 events

Parameters:
  FunctionName:
    Type: String
    Description: "The name of the Lambda function"
  FunctionTimeout:
    Type: Number
    Description: "The maximum time in seconds that the function can run before it is stopped"
    Default: 3
  FunctionMemorySize:
    Type: Number
    Description: "The size of the memory in MB allocated per invocation of the function"

Resources:
  MyS3LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Ref FunctionName
      CodeUri: ../functions/
      Handler: app.lambda_handler
      Runtime: python3.12
      MemorySize: !Ref FunctionMemorySize
      Timeout: !Ref FunctionTimeout
      Policies:
        - S3WritePolicy:
            BucketName: !ImportValue MyS3BucketName
      Events:
        S3Event:
          Type: S3
          Properties:
            Bucket: !ImportValue MyS3BucketName
            Events: s3:ObjectCreated:*
      Tags:
        Name: !Ref FunctionName

Outputs:
  MyS3LambdaFunctionArn:
    Description: "The ARN of the Lambda Function"
    Value: !GetAtt MyS3LambdaFunction.Arn
    Export:
      Name: MyS3LambdaFunctionArn

I want to know if it's possible to create S3 and Lambda using cross-stack references with AWS SAM. If it's possible, I would like to know how to fix it.

asked 2 months ago56 views
1 Answer
0

The error message "AWS::Serverless-2016-10-31 is null" suggests that there's an issue with the SAM transform in your templates. While it is possible to create S3 and Lambda resources using cross-stack references with AWS SAM, there are a few adjustments needed in your current setup:

  1. The SAM transform should be added to the root template, not just the Lambda template. Add the following line at the top of your root-template.yaml:

    Transform: AWS::Serverless-2016-10-31
  2. When using nested stacks with SAM, you need to use the AWS::Serverless::Application resource type instead of AWS::CloudFormation::Stack for the nested stacks that use SAM features. Update your root-template.yaml:

    LambdaStack:
      Type: AWS::Serverless::Application
      DependsOn:
        - S3Stack
      Properties:
        Location: ./lambda.yaml
        Parameters:
          FunctionName: !Ref FunctionName
          FunctionTimeout: !Ref FunctionTimeout
          FunctionMemorySize: !Ref FunctionMemorySize
  3. For the S3 stack, you can keep using AWS::CloudFormation::Stack since it doesn't use SAM features.

  4. In your lambda.yaml, remove the Transform line as it's now handled by the root template.

  5. When packaging your application, make sure to use the --resolve-s3 option with the sam package command to automatically create an S3 bucket for your deployment artifacts:

    sam package --template-file templates/root-template.yaml --output-template-file packaged.yaml --resolve-s3
    
  6. Then deploy using:

    sam deploy --template-file packaged.yaml --stack-name your-stack-name --capabilities CAPABILITY_IAM
    

These changes should resolve the "AWS::Serverless-2016-10-31 is null" error and allow you to create S3 and Lambda resources using cross-stack references with AWS SAM. Remember to adjust the CodeUri in your lambda.yaml to point to the correct location of your function code relative to the root template.
Sources
sam deploy - AWS Serverless Application Model
sam package - AWS Serverless Application Model
Process Amazon S3 events with AWS SAM - AWS Serverless Application Model

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions