0

In all the examples I can find, it seems that the SAM template creates a new DynamoDB table. How can I configure it to point to existing tables?

yasgur99
  • 756
  • 2
  • 11
  • 32

2 Answers2

3

Since the resources already exist, you can hard-code the ARNs for the tables where you would normally reference the tables by their CloudFormation logical names (if they had been created by CloudFormation).

For example, if you're giving scan permission to a table called Example, you could create a parameter:

Parameters:
  ExampleTableArn:
    Description: Example DynamoDB table ARN
    Type: String
    Default: arn:aws:dynamodb:us-west-2:xxxxxxxxxxxx:table/Example

And then in your Lambda policy:

Policies:
  Version: '2012-10-17'
  Statement:
  - Effect: Allow
    Action:
    - 'dynamodb:Scan'
    Resource: {Ref: ExampleTableArn}
Tom
  • 1,660
  • 8
  • 16
1

You do not need to set up table ARN if you use the policy template in the Policy template list.

template.yaml

# ====================================
# TODO: SETUP PARAMETERS
# ====================================

Parameters:
  ExistingTable:
    Type: String
    Default: example-table
    Description: (Required) The name of existing DynamoDB
    MinLength: 3
    MaxLength: 50
    AllowedPattern: ^[A-Za-z_-]+$
    ConstraintDescription: "Required. Can be characters, hyphen, and underscore only. No numbers or special characters allowed."


# ====================================
# TODO: SETUP FUNCTIONS
# ====================================

  OnConnectFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/lambdas/connect.handler
      MemorySize: 256
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ExistingTable

Explain:

In this template.yaml, I set up the parameter ExistingTable to allow input existing table name. In function, I used DynamoDBCrudPolicy that allows creating, retrieving, updating, and deleting on the existing table.

Long Nguyen
  • 9,898
  • 5
  • 53
  • 52