I'm trying to set up a local dev environment for my Lambda functions using SAM. I had everything working until I added a reference to a layer in my configuration.
I followed the instructions here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html. I added my ARN for my layer version in my template.ymal as follows:
# template.ymal
TestLayerFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: TestLayer
Role: arn:aws:iam::111111111111:role/ReadStreamingTable
CodeUri: src/streaming/test-layer/
Handler: app.handler
Runtime: nodejs8.10
Layers:
- arn:aws:lambda:eu-west-1:111111111111:layer:Global:7
However when running a "sam local invoke" I get the following error:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:
"https://lambda.eu-west-1a.amazonaws.com/2018-10-31/layers/arn%3Aaws%3Alambda%3Aeu-west-1%3A111111111111%3Alayer%3AGlobal/versions/7"
The way I've added the layer ARN in the configuration seems to be exactly how they do it in the example so I'm not sure what is causing the error.
I know it's not exactly a solution but can you not have your layer as part of your SAM file?
If you have a look on this article on the AWS site they use both the layer and the lambda function on the same yaml file so you'd end up with something like this:
Resources:
TempConversionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Layers:
- !Ref TempConversionDepLayer
Events:
HelloWorld:
Type: Api
Properties:
Path: /{conversion}/{value}
Method: get
TempConversionDepLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sam-app-dependencies
Description: Dependencies for sam app [temp-units-conv]
ContentUri: dependencies/
CompatibleRuntimes:
- nodejs6.10
- nodejs8.10
LicenseInfo: 'MIT'
RetentionPolicy: Retain
Related
I have an AWS SAM template defining, amongst many other things, a JavaScript Lambda:
Resources:
notesFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Zip
CodeUri: notes/
Handler: app.lambdaHandler
Runtime: nodejs18.x
Policies:
- AmazonDynamoDBFullAccess
Architectures:
- x86_64
Events:
Fetchnotes:
Type: Api
Properties:
Path: /notes
Method: get
Givenotes:
Type: Api
Properties:
Path: /notes
Method: post
Users:
Type: Api
Properties:
Path: /notes/users
Method: get
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: "es2020"
Sourcemap: true
EntryPoints:
- app.ts
Later on in the template, I am trying to reference this Lambda's role (example: Role: !Ref <MyLambdaRole>) but not sure how to do that, since the role is created on the fly when deploying the SAM template (CloudFormation stack). Any ideas how I can do this?
If you do not provide a role in your AWS::Serverless::Function definition, SAM creates a role with a Logical ID of <function‑LogicalId>Role.
In your case, this would be !Ref notesFunctionRole.
Pretty new to AWS Lambda function, and this is my time to get my hands dirty. I got this error in the title when I wanted to docker build my function. And here is how I configured my function:
PitchAiIngest:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub pitch-ai-ingest-${Environment}
Handler: lambda_function.lambda_handler
Runtime: python3.7
CodeUri: pitchai_ingest/
Description: get pitchai information from API and publish to dynamodb
MemorySize: 128
Timeout: 900
Role: !GetAtt LambdaRole.Arn
Environment:
Variables:
LOGGING_LEVEL: INFO
APP_NAME: pitch-ai-ingest
APP_ENV: !Ref Environment
DYNAMO_DB: !Ref PitchAiEventDynamoDBTable
PLAYER_DB: !Ref PitchAiPlayerDynamoDBTable
PITCH_SQS: !Ref PitchAiIngestQueue
Tags:
env: !Ref Environment
service: pitch-ai-service
function_name: !Sub pitch-ai-ingest-${Environment}
Roughly speaking, I post the snippet above in file cfn-tempate.yml under the same directory of folder pitchai_ingest (including Lambda handler).
What should I do to fix it?
I mistakenly set AWS_ACCESS_KEY_ID as AWS_ACCESS_KEY. That's why the credential wasn't found.
I am using AWS SAM to test my Api gateway and lambdas locally.
When executing sam local start-api and calling a lambda, I'd like the event to be of version 2.0 format instead of version 1.
I am using CDK HttpApi construct from #aws-cdk/aws-apigatewayv2 hence there is now inconsistency between my local testing and what's deployed.
I am new to Sam config, my template.yml file is:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: .output/healthz
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /health
Method: GET
Globals:
HttpApi:
CorsConfiguration:
AllowOrigin: "'http://localhost:3000'"
AllowMethods: "'POST, OPTIONS, GET, PUT'"
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
I've tried various setups (Api, HttpApi) using those AWS Docs for SAM but always manage to get only version 1 event.
Can you point me to what I am doing wrong or how to specify the version?
I found the resolution in this post.
It is necessary to specify PayloadFormatVersion: "2.0" (value as string) in the configuration of HttpApi.
Example yml:
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: .output/healthz
Timeout: 10
Events:
ApiEvent:
Type: HttpApi
Properties:
PayloadFormatVersion: "2.0"
Path: /health
Method: GET
Auth:
Authorizer: NONE
So far I have this template.yml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
My Lambda for doing something
Resources:
FirstLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: FirstLayer
Description: First layer of dependencies
ContentUri: layers/first-layer/
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
SecondLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: SecondLayer
Description: Second layer of dependencies
ContentUri: layers/second-layer/
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
MyLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: "MyLambda"
Policies:
- AmazonS3FullAccess
CodeUri: src/
Handler: lambda.handler
Timeout: 30
MemorySize: 2048 # Chrome will require higher memory
Runtime: nodejs14.x
Layers:
- !Ref FirstLayer
- !Ref SecondLayer
With this template I am able to start and invoke MyLambda locally and also deploy it to AWS. The problem I have is that I would like to reuse these same layers on other Lambdas as well, so for doing that I could simply extract these layers to another yml file, deploy them separately and then include the layers ARNs in the Layers property of my Lambda, but then, how can I run it locally with sam? I wouldn't like to have 2 template.yml files for my Lambda, one including the Layers on the Resources (like the one I already have) to run locally and another one with the refs to the actual layers ARNs to deploy on AWS, but that's the only solution I am seeing now.
The first question you need to ask is if those lambdas belong to the same application. If that´s not the case, you should use different templates, in order to deploy different stacks, to have isolated environments.
However, if you want to share resources, you have to very similar options:
Configure the layer in the parent template and pass the ARN as a parameter.
template.yml
Resources:
SharedLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared_layer
Description: Some code to share with the other lambda functions
ContentUri: ./layer
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Application:
Type: "AWS::Serverless::Application"
Properties:
Location: "./app.template.yml"
Parameters:
SharedLayer: !Ref SharedLayer
app.template.yml
Parameters:
SharedLayer:
Type: String
Description: ARN of the SharedLayer
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: index.handler
Layers:
- !Ref SharedLayer
Configure the layers in a nested template, set the ARN as an output, and then pass its output as a parameter to the other templates.
layers.template.yml
Resources:
SharedLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared_layer
Description: Some code to share with the other lambda functions
ContentUri: ./layer
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Outputs:
SharedLayerARN:
Description: ARN of the Shared Layer
Value: !Ref SharedLayer
template.yml
Layer:
Type: "AWS::Serverless::Application"
Properties:
Location: "./layers.template.yml"
Application:
Type: "AWS::Serverless::Application"
Properties:
Location: "./app.template.yml"
Parameters:
SharedLayer: !GetAtt Layer.Outputs.SharedLayerARN
Both scenarios are supported by AWS SAM.
I created following yml file to deploy with SAM in AWS. But when I added the Role it fails with error "UPDATE_ROLLBACK_COMPLETE. Reason: No reason was provided.". Why does it happens?
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'awscodestar-${ProjectId}-lambda-HelloWorld'
Handler: index.handler
Runtime: python3.7
Role: arn:aws:iam::790615885331:role/lambda_dynam
Events:
GetEvent:
Type: Api
Properties:
Path: /
Method: get
PostEvent:
Type: Api
Properties:
Path: /
Method: post
It fails if I completely remove the Role and it works only if I added following expression as the Role
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
I want to add a already created role.