I have a Lambda function declared in serverless.yml and it needs an environment variable which is the table name of Dynamodb table. Under resources, I create the DynamoDB Table. But while “sls deploy”, the function is created first and then the DynamoDB table. Is there any way I can provide DependsOn for the functions?
You should be able to but you also shouldn’t need to. This is how I do it.
service: demo # NOTE: update this with your service name
provider:
stage: ${opt:stage, "dev"}
environment:
POSTS_TABLE: ${self:custom.postsTableName}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
Resource:
Fn::GetAtt:
- PostsTable
- Arn
custom:
postsTableName: ${self:service}-posts-${self:provider.stage}
functions:
get:
handler: get.getHandler
events:
- http:
path: posts/{id}
method: get
resources:
Resources:
PostsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.postsTableName}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
Do you know how to define a dependency for a function? I’ve a function that depends on a VPC that has a VPCEndpoint which gets deleted before my function.
functions:
dbMigration:
handler: lambda-bin/migrate-db
environment:
DB_MASTER_USER: ${env:DB_MASTER_USER}
DB_MASTER_PASSWORD: ${env:DB_MASTER_PASSWORD}
DB_ADDRESS:
"Fn::GetAtt": [ SheltersDB, Endpoint.Address ]
vpc:
securityGroupIds:
- Ref: SheltersVPCSecurityGroup
subnetIds:
- Ref: SheltersSubnet1
- Ref: SheltersSubnet2
You can see the whole config at https://github.com/hitta-skyddsrum/update-shelters/blob/83225bfb8f13aaafd88c96236daed3993e71cc86/serverless.yml
I guess it may be a bug within serverless though, since the VPCEndpoint should be dependent on the VPC.