Lambda Layer with SAM CLI - python-3.x

I created a lambda layer with serverless successfully but now I have to do the same with SAM CLI but i can't.
With serverless I only use two files:
serverless.yml
awswrangler-layer-0.0.23-py3.7.zip
serverless.yml content below:
service: MyService
provider:
name: aws
layers:
awswrangler:
package:
artifact: awswrangler-layer-0.0.23-py3.7.zip
How can I do the same with SAM CLI? Please give an example of the template.yaml

I have to unzip and this was the solution:
AwswranglerLayer :
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Join ['-', [!Ref Project, !Ref Environment, 'AwswranglerLayer']]
ContentUri: ../layers/awswrangler/
Description: "ETL and wrangling utility belt to handle data on AWS. Pandas, PySpark"
CompatibleRuntimes:
- python3.7
- python3.8
RetentionPolicy: Retain

AwswranglerLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub '${EnvironmentKey}-AwswranglerLayer'
CompatibleRuntimes:
- nodejs12.x
ContentUri: AwswranglerLayerPath/

Related

AWS lambda can't find lambda layer path

I am having a tough time setting up the lambda layers with lambdas. I am using node 14.
My folder structure for lambda layer
layer/
nodejs/
node14/
node_modules/
hey.js
I have also tried having only the nodejs directory as below
layer/
nodejs/
hey.js
But in both cases I get cannot found module error in the lambda.
The paths I tried for accessing layers in lambda are as below
'/opt/nodejs/node14/node_modules/hey.js' (for first folder structure)
'/opt/nodejs/hey.js' (for folder structure with only the nodejs directory in the layer)
'hey.js' (trying to access the file directly)
But I had no luck. What am I doing wrong?
I am using AWS sam to deploy lambda and layers. I could see the layer getting attached to lambda on the console.
Here is my SAM template
layer1:
Type: AWS::Serverless::LayerVersion
Properties:
ContentUri: ./src/lambda-layers/layer1
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
lambda1:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: ./src/lambdas/lambda1
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Layers:
- !Ref layer1
Events:
AwsIoTMetadata:
Type: Api
Properties:
RestApiId: !Ref CrApi
Path: /user
Method: GET
How to access the layer in lambda?
Please Help. Thanks in advance

Serverless: Layers No file matches include / exclude patterns

My serverless.yml file works fine as soon as I add layers I starting getting this error
Serverless Error ----------------------------------------
No file matches include / exclude patterns
service: foundation
useDotenv: true
custom:
name: foundation
provider:
name: aws
stackName: ${self:service}-${self:provider.stage}
region: us-east-1
stage: ${opt:stage, 'dev'}
environment:
REGION: ${self:provider.region}
STAGE: ${self:provider.stage}
layers:
certificates:
path: certificate
plugins:
- serverless-deployment-bucket
- serverless-pseudo-parameters
- serverless-plugin-typescript
functions:
- ${file(./src/handler/function.yml)}
resources:
- ${file(./resources/outputs.yml)}
runtime: Node.js
Note: using layers to add certificates to lambda
Serverless version: 2.31.0
You may be getting this error if you execlude the directory where the layer is running through the plugins you use or in another part of the yml file. Maybe editing it this way will solve the problem
this link has an explanation about it
layers:
certificates:
package:
include:
- ./your/layer/path/**

How to access SSM Parameter Store from SAM lambda local in node

I have a lambda with node and for local deployment I am using SAM CLI. This lambda requires some parameters in the SSM parameter store to be able to connect to the DB.
I configured the AWS_ACCES_KEY_ID and AWS_SECRET_ACCESS_KEY, as environment variables, in addition to the region. When executing the local lamda, I do not get any error, as it goes to aws, but it does not bring me anything. It is not a code issue, because if I deploy it already in aws it works without problem. I don't know if I need to do another configuration for it to work.
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
ciencuadras-appraisal-request
Sample SAM Template for ciencuadras-appraisal-request
Parameters:
Stage:
Type: String
Default: dev
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
ApiDeployment:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Stage
RequestAppraisalFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: dist/
Handler: main.handler
Runtime: nodejs14.x
Environment:
Variables:
AWS_REGION: 'us-east-1'
Events:
RequestAppraisal:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /sendemail-new-appraisal
Method: post
RestApiId: !Ref ApiDeployment
Thanks
Currently there is no possibility to access Parameter Store variables from Sam Local as you can read up here.
Instead, you can use --env-vars option on SAM CLI to pass values to
the running function.
You can still use SSM in your template and pass --env-vars when invoking the function locally, for example:
template.yaml
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: dist/
Handler: main.handler
Runtime: nodejs14.x
Environment:
Variables:
API_KEY: !Sub '{{resolve:ssm:API_KEY:1}}'
env.json:
{
"Parameters": {
"API_KEY": "123"
}
}
pass env.json when invoking the function:
sam local invoke --env-vars env.json MyFunction
Note: If you are running API locally too, you can do:
sam local start-api --env-vars env.json

AWS SAM fails to build layer

I have read that SAM now supports building layers and followed the directions mentioned here. However, i am getting a build error when i try to build the layer locally with sam build samDeployLayer
Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "E:\Development\sam-deploy\src\sam-deploy-layer" as it does not contain a package.json file.
Here is my template file:
AWSTemplateFormatVersion: 2010-09-09
Description: >-
sam-deploy
Transform:
- AWS::Serverless-2016-10-31
Resources:
samDeploy:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/sam-deploy
Handler: index.handler
Runtime: nodejs12.x
MemorySize: 128
Timeout: 100
Description: A Lambda function that returns a static string.
FunctionName: "sam-deploy"
Layers:
- !Ref samDeployLayer
Policies:
# Give Lambda basic execution Permission to the helloFromLambda
- AWSLambdaBasicExecutionRole
samDeployLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sam-deploy-layer
ContentUri: src/sam-deploy-layer
CompatibleRuntimes:
- nodejs12.x
Metadata:
BuildMethod: nodejs12.x
I have included both the CompatibleRuntimes and Metadata properties as per the requirement to build layers. package.json for this layer is located at src/sam-deploy-layer/nodejs as required for nodeJS runtimes and i am using SAM CLI version 0.53.0. What am i doing wrong?
Place package.json under src/sam-deploy-layer/.
After running sam build, the directory .aws-sam/build/samDeployLayer/nodejs/node_modules/ should be created.
You should create a folder named nodejs in src/sam-deploy-layer. Here, you can do npm init and npm install (packages in your layer).

How to set up environment variables when using aws-serverless-express

I am using aws-serverless-express for deploying an express api on aws lambda. I followed the aws-serverless-express repository example (https://github.com/awslabs/aws-serverless-express/tree/master/examples/basic-starter) to deploy the api and it works, but now I don't know how to set up environment variables in express code then after the express deployment I can see and edit those environment variables on lambda console. I didn't find any documentation about this.
In the repository you mentioned, the cloudformation.yaml file has the function definition called YOUR_SERVERLESS_EXPRESS_LAMBDA_FUNCTION_NAME. you can define an attribute called Environment under that. see the example below.
YOUR_SERVERLESS_EXPRESS_LAMBDA_FUNCTION_NAME:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: lambda.handler
MemorySize: 1024
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs8.10
Timeout: 30
Environment:
Variables:
SOME_VAR: value
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /{proxy+}
Method: ANY

Resources