Skip to content

Fragment returned by transform AWS::Serverless-2016-10-31 is null

0

I have no clue what is wrong with this template, any fix i found on internet is not working

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Provision the Lambda infrastructure for Someproject

Parameters:
  EnvironmentName:
    Type: String
    Default: dev
    Description: Refer the current environment
    AllowedValues: ['dev', 'prod', 'preprod']
    ConstraintDescription: Select 'dev' or 'prod' or 'preprod'

Conditions:
  IsProdEnvironment: !Equals [ !Ref EnvironmentName, prod ]

Resources:
  LambdaFunction:
    Type: 'AWS::Serverless::Function'
    DependsOn: LambdaFunctionLogGroup
    Properties:
      FunctionName: !Sub "someproject-api-${EnvironmentName}"
      Description: Provision the infrastructure for someproject-api
      Runtime: python3.12
      CodeUri: ../someproject_build
      Handler: run.handler
      Timeout: !If [IsProdEnvironment, 900, 300]
      Policies:
        - AWSLambdaBasicExecutionRole
        - PolicyName: S3AccessPolicyForLambda
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:PutObjectAcl
                  - s3:GetObject
                  - s3:GetObjectAcl
                  - s3:ListBucket
                  - s3:GetBucketLocation
                  - s3:ListBucketMultipartUploads
                  - s3:ListMultipartUploadParts
                  - s3:AbortMultipartUpload
                  - s3:CreateBucket
                Resource:
                  - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}/*"
                  - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}"
      Environment:
        Variables:
          ENVIRONMENT: !Ref EnvironmentName
          BUCKET_NAME: !Sub "someproject-bucket-${EnvironmentName}"
      Tags:
        - Key: Application
          Value: someproject
        - Key: CostCenter
          Value: someproject
        - Key: environment
          Value: !Ref EnvironmentName

  LambdaFunctionLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/lambda/someproject-api-${EnvironmentName}"
      RetentionInDays: !If [IsProdEnvironment, 180, 14]

asked 2 years ago314 views

2 Answers
2
Accepted Answer

Hello.

Please make the template as below.
Your template specifies something called "PolicyName", which is probably the problem.
I was able to deploy it by modifying "Policies" as shown below.
Also, since "Tags" of "AWS::Serverless::Function" is a map, an error would occur if it were a Key Value, so I modified it to map format.
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-tags

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Provision the Lambda infrastructure for Someproject

Parameters:
  EnvironmentName:
    Type: String
    Default: dev
    Description: Refer the current environment
    AllowedValues: ['dev', 'prod', 'preprod']
    ConstraintDescription: Select 'dev' or 'prod' or 'preprod'

Conditions:
  IsProdEnvironment: !Equals [ !Ref EnvironmentName, prod ]

Resources:
  LambdaFunction:
    Type: 'AWS::Serverless::Function'
    DependsOn: LambdaFunctionLogGroup
    Properties:
      FunctionName: !Sub "someproject-api-${EnvironmentName}"
      Description: Provision the infrastructure for someproject-api
      Runtime: python3.12
      CodeUri: ../someproject_build
      Handler: run.handler
      Timeout: !If [IsProdEnvironment, 900, 300]
      Policies:
        - AWSLambdaBasicExecutionRole
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - s3:PutObject
                - s3:PutObjectAcl
                - s3:GetObject
                - s3:GetObjectAcl
                - s3:ListBucket
                - s3:GetBucketLocation
                - s3:ListBucketMultipartUploads
                - s3:ListMultipartUploadParts
                - s3:AbortMultipartUpload
                - s3:CreateBucket
              Resource:
                - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}/*"
                - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}"
      Environment:
        Variables:
          ENVIRONMENT: !Ref EnvironmentName
          BUCKET_NAME: !Sub "someproject-bucket-${EnvironmentName}"
      Tags:
        Application: "someproject"
        CostCenter: "someproject"
        environment: !Ref EnvironmentName

  LambdaFunctionLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/lambda/someproject-api-${EnvironmentName}"
      RetentionInDays: !If [IsProdEnvironment, 180, 14]

If you want to specify the name of the IAM policy, I think you should create a custom IAM role and specify it using "Role" instead of "Policies".

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Provision the Lambda infrastructure for Someproject

Parameters:
  EnvironmentName:
    Type: String
    Default: dev
    Description: Refer the current environment
    AllowedValues: ['dev', 'prod', 'preprod']
    ConstraintDescription: Select 'dev' or 'prod' or 'preprod'

Conditions:
  IsProdEnvironment: !Equals [ !Ref EnvironmentName, prod ]

Resources:
  FunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Action: "sts:AssumeRole"
            Principal:
              Service: lambda.amazonaws.com
      Policies:
        - PolicyName: "S3AccessPolicyForLambda"
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:PutObjectAcl
                  - s3:GetObject
                  - s3:GetObjectAcl
                  - s3:ListBucket
                  - s3:GetBucketLocation
                  - s3:ListBucketMultipartUploads
                  - s3:ListMultipartUploadParts
                  - s3:AbortMultipartUpload
                  - s3:CreateBucket
                Resource:
                  - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}/*"
                  - !Sub "arn:aws:s3:::someproject-bucket-${EnvironmentName}"
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  LambdaFunction:
    Type: 'AWS::Serverless::Function'
    DependsOn: LambdaFunctionLogGroup
    Properties:
      FunctionName: !Sub "someproject-api-${EnvironmentName}"
      Description: Provision the infrastructure for someproject-api
      Runtime: python3.12
      CodeUri: ../someproject_build
      Handler: run.handler
      Timeout: !If [IsProdEnvironment, 900, 300]
      Role: !GetAtt FunctionRole.Arn
      Environment:
        Variables:
          ENVIRONMENT: !Ref EnvironmentName
          BUCKET_NAME: !Sub "someproject-bucket-${EnvironmentName}"
      Tags:
        Application: "someproject"
        CostCenter: "someproject"
        environment: !Ref EnvironmentName

  LambdaFunctionLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/lambda/someproject-api-${EnvironmentName}"
      RetentionInDays: !If [IsProdEnvironment, 180, 14]
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thanks, this fix the issue. I was not awre of what exactly was wrong till you comment. You saved my day! Merry Xmas

-2

The error "Fragment returned by transform AWS::Serverless-2016-10-31 is null" typically indicates an issue with the AWS Serverless Application Model (SAM) template. Based on the provided template, there are a few potential issues that could be causing this error:

  1. CodeUri: The CodeUri property is pointing to "../someproject_build". Ensure that this path is correct relative to where you're deploying the template from. If the path is incorrect or the directory doesn't exist, it could cause this error.

  2. Runtime: The template specifies "python3.12" as the runtime. As of my last update, AWS Lambda doesn't support Python 3.12. Try using a supported Python version like "python3.9" or "python3.10".

  3. Policies: The custom policy defined in the Policies section looks correct, but make sure that all the S3 actions listed are actually needed for your function.

  4. DependsOn: The LambdaFunction resource has a DependsOn attribute referencing LambdaFunctionLogGroup. While this is not incorrect, it's usually not necessary as AWS SAM typically handles these dependencies automatically.

  5. Environment Variables: Ensure that the environment variables, especially BUCKET_NAME, are correctly defined and used within your Lambda function code.

To resolve this issue:

  1. Update the Runtime to a supported Python version.
  2. Double-check the CodeUri path to ensure it's correct.
  3. Verify that all the S3 permissions in the custom policy are necessary.
  4. Consider removing the DependsOn attribute if it's not absolutely necessary.
  5. Ensure that your Lambda function code (in the someproject_build directory) is complete and error-free.

If the issue persists after making these changes, you may want to try deploying a simplified version of the template first, and then gradually add back the more complex elements to isolate the problem.
Sources
AWS::Serverless::Api - AWS Serverless Application Model
Configure Lambda function timeout - AWS Lambda

answered 2 years ago

  • None of this fix the issue

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.