0

I am using the serverless framework to create a DynamoDB table and then I want to access it from a Lambda function.

In the serverless.yml file I have the definitions below for the environment variable and CF resources.

What I was expecting was a table with the name accounts-api-dev-accounts, but what the cloudformation stack is creating for me is accounts-api-dev-accounts-SOME_RANDOM_LETTERS_AND_NUMBERS_SUFFIX.

In my lambda function the environment variable DYNAMODB_ACCOUNTS_TABLE_NAME is exposed to the function without the SOME_RANDOM_LETTERS_AND_NUMBERS_SUFFIX part. Is the CF stack supposed to add a random suffix? How do I actually retrieve the right table name?

service:
  name: accounts-api
provider:
...
  stage: ${opt:stage, 'dev'}
  environment:
    DYNAMODB_ACCOUNTS_TABLE_NAME: '${self:service}-${self:provider.stage}-accounts'

And the following CF resource:

  Resources:
      AccountsTable:
          Type: AWS::DynamoDB::Table
          Properties:
            TableName: ${env:DYNAMODB_ACCOUNTS_TABLE_NAME}
            AttributeDefinitions:
              - AttributeName: customerNumber
                AttributeType: S
              - AttributeName: accountNumber
                AttributeType: S
            KeySchema:
              - AttributeName: customerNumber
                KeyType: HASH
              - AttributeName: accountNumber
                KeyType: RANGE
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
3
  • Another comment, I get the following warning when deploying: Serverless Warning -------------------------------------- A valid environment variable to satisfy the declaration 'env:DYNAMODB_ACCOUNTS_TABLE_NAME' could not be found. Commented Jul 8, 2020 at 12:13
  • 1
    maybe the environment variables are not updated yet at the time of the creation of the table definition? I'm not sure. try ${self:provider.environment.DYNAMODB_ACCOUNTS_TABLE_NAME} instead of ${env:DYNAMODB_ACCOUNTS_TABLE_NAME} Commented Jul 8, 2020 at 13:40
  • 1
    Bingo. Not sure why I can't use ${env} with it, but it works. Commented Jul 8, 2020 at 13:48

2 Answers 2

3

Maybe the environment variables are not updated yet at the time of the creation of the table definition? I'm not sure.

Try ${self:provider.environment.DYNAMODB_ACCOUNTS_TABLE_NAME} instead of ${env:DYNAMODB_ACCOUNTS_TABLE_NAME}.

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't seen this behavior yet (random characters after deploy), it could be a way to force uniqueness when the table has to be replaced. You could use another environment variable and have the value populated by the Table resource's output. That way, CloudFormation will inject the actual resource name to the Lambda environment variable. I haven't tried this, but this would be my first "go to".

environment:
  DYNAMODB_ACCOUNTS_TABLE_NAME: '${self:service}-${self:provider.stage}-accounts'
  ACTUAL_DYNAMODB_ACCOUNTS_TABLE_NAME:
    Ref: AccountsTable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.