Skip to content

Instantly share code, notes, and snippets.

@hareku
Last active December 20, 2021 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hareku/30023706e854015bfa289bd4a2081022 to your computer and use it in GitHub Desktop.
Save hareku/30023706e854015bfa289bd4a2081022 to your computer and use it in GitHub Desktop.
hareku/go-timestreamer with aws-sam template

How to deploy

mkdir myapp
cd myapp

# clone lambda functions
git clone https://github.com/hareku/go-timestreamer

# create and copy template for AWS SAM
vi template.yaml

# deploy by AWS SAM CLI
sam deploy
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: MyCountingRecorder
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 10
Api:
Auth:
ApiKeyRequired: true
UsagePlan:
CreateUsagePlan: SHARED
Description: Usage plan for this API
Quota:
Limit: 30
Period: DAY
Parameters:
TimestreamDatabaseName:
Type: String
Default: mydatabase
Description: "The Timstream database name."
TimestreamTableName:
Type: String
Default: mytable
Description: "The Timstream table name."
TimestreamMeasureName:
Type: String
Default: mymeasure
Description: "The Timstream measurement name."
Resources:
TimestreamRecorderFunction:
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: go-timestreamer/cmd/simple/lambda/record/
Handler: record
Runtime: go1.x
Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
Events:
CatchAll:
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: /record
Method: GET
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
DATABASE_NAME: !Ref TimestreamDatabaseName
TABLE_NAME: !GetAtt TimestreamTable.Name
MEASURE_NAME: !Ref TimestreamMeasureName
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- timestream:WriteRecords
Resource: !GetAtt TimestreamTable.Arn
- Effect: Allow
Action:
- timestream:DescribeEndpoints
Resource: '*'
TimestreamDatabase:
Type: AWS::Timestream::Database
Properties:
DatabaseName: !Ref TimestreamDatabaseName
TimestreamTable:
Type: AWS::Timestream::Table
Properties:
DatabaseName: !Ref TimestreamDatabase
TableName: !Ref TimestreamTableName
RetentionProperties:
MagneticStoreRetentionPeriodInDays: 73000
MemoryStoreRetentionPeriodInHours: 1
ReportingTopic:
Type: AWS::SNS::Topic
ReportingTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: myemail@example.com
Protocol: email
TopicArn: !Ref ReportingTopic
DailyReportFuncion:
Type: AWS::Serverless::Function # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
CodeUri: go-timestreamer/cmd/simple/lambda/publish-sns/
Handler: publish-sns
Runtime: go1.x
Tracing: Active # https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
Events:
Schedule:
Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html
Properties:
Description: Schedule to run the daily reporting
Enabled: True
Schedule: "cron(1 15 * * ? *)"
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
DATABASE_NAME: !Ref TimestreamDatabaseName
TABLE_NAME: !GetAtt TimestreamTable.Name
SNS_TOPIC_ARN: !Ref ReportingTopic
GO_TIMEZONE: Asia/Tokyo
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- sns:Publish
Resource: !Ref ReportingTopic
- Effect: Allow
Action:
- timestream:Select
Resource: !GetAtt TimestreamTable.Arn
- Effect: Allow
Action:
- timestream:DescribeEndpoints
- timestream:SelectValues
Resource: '*'
Outputs:
TimestreamRecorderAPI:
Description: "API Gateway endpoint URL for Prod environment to Record"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/record"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment