When i use SAM to create my Lambda,API gateway and DynamoDB table it all works until i get to the actual created table. It is supposed to be called "List" however after the word "list" it gives me a bunch of random numbers and letters. What i want to do is when all 3 of the services are created they should talk to eachother however since i am getting this problem, I have to manually add the name to my function for it to work.
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
ClickSam:
Type: 'AWS::Serverless::Function'
Properties:
Handler: app.lambda_handler
Runtime: python3.8
CodeUri: Lambda/
MemorySize: 128
Timeout: 3
Environment:
Variables:
TABLE_NAME: !Ref List
REGION_NAME: !Ref AWS::Region
Events:
ClickAPI:
Type: Api
Properties:
Path: /visits
Method: GET
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref List
List:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: url
Type: String
---------- This is my Lambda Function code to create items for the table.
import boto3
import json
def lambda_handler(event, context):
dynamodb = boto3.client('dynamodb')
response = dynamodb.update_item(
TableName='List',
Key={
'url':{'S': "etc.com"}
},
UpdateExpression='ADD visits :inc',
ExpressionAttributeValues={
':inc': {'N': '1'}
},
ReturnValues="UPDATED_NEW"
)
response = {
'statusCode': 200,
'headers': {
"Content-Type" : "application/json",
"Access-Control-Allow-Origin" : "*",
"Allow" : "GET, OPTIONS, POST",
"Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
"Access-Control-Allow-Headers" : "*"
},
'body': json.dumps(int(response["Attributes"]["visits"]["N"]))
}
return response
Use TableName
parameter to fix the table name (cf. AWS::Serverless::SimpleTable).
Type: AWS::Serverless::SimpleTable
Properties:
TableName: MyTable
Documentation of the AWS::DynamoDB::Table resource :
If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the table name.