- Newest
- Most votes
- Most comments
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]
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:
-
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.
-
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".
-
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.
-
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.
-
Environment Variables: Ensure that the environment variables, especially BUCKET_NAME, are correctly defined and used within your Lambda function code.
To resolve this issue:
- Update the Runtime to a supported Python version.
- Double-check the CodeUri path to ensure it's correct.
- Verify that all the S3 permissions in the custom policy are necessary.
- Consider removing the DependsOn attribute if it's not absolutely necessary.
- 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
Relevant content
- AWS OFFICIALUpdated 3 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