module.exports = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "A simple streambot example",This template sets up a Lambda function that simply reads records from a Kinesis stream, and writes those records to S3. Your service may operate entirely differently. It may define other resources, it may use another method to invoke the Lambda function. This is only meant to provide an example of one possible configuration.
module.exports = {
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "A simple streambot example",When starting the stack you provide various options that will become runtime configuration for the Lambda Function
"Parameters": {
"GitSha": {
"Type": "String",
"Description": "The Streambot GitSha to which this example pertains"
},
"EventBucket": {
"Type": "String",
"Description": "The S3 bucket where events will be written"
},
"EventPrefix": {
"Type": "String",
"Description": "The S3 prefix where events will be written"
},
"StreambotEnvFunctionArn": {
"Type": "String",
"Description": "The ARN for the StreambotEnv function set up by the primary Streambot template"
},
"StreambotConnectorFunctionArn": {
"Type": "String",
"Description": "The ARN for the StreambotConnector function set up by the primary Streambot template"
}
},Five resources are created:
"Resources": {A Kinesis stream with one shard. Records in this stream with trigger invocation of the primary Lambda function.
"Stream": {
"Type": "AWS::Kinesis::Stream",
"Properties": {
"ShardCount": 1
}
}, "Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"Path": "/streambot/", "AssumeRolePolicyDocument": {
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{Defines the permissions that the Lambda function will have once it has assumed this role.
"PolicyName": "StreambotExamplePolicy",
"PolicyDocument": {
"Statement": [ {
"Effect": "Allow",
"Action": [
"logs:*"
],
"Resource": "arn:aws:logs:*:*:*"
}, {
"Effect": "Allow",
"Action": [
"dynamodb:GetItem"
],
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:dynamodb:us-east-1:",
{
"Ref": "AWS::AccountId"
},
":table/streambot-env*"
]
]
}
}, {
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "EventBucket"
},
"/",
{
"Ref": "EventPrefix"
},
"*"
]
]
}
}, {
"Effect": "Allow",
"Action": [
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:ListStreams"
],
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:kinesis:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":stream/",
{
"Ref": "Stream"
}
]
]
}
}
]
}
}
]
}
},This is the Lambda function that defines this example service (see streambot-example/index.js). It reads records from a Kinesis stream and writes them to S3.
"Function": {
"Type" : "AWS::Lambda::Function",
"Properties" : { "Code" : {
"S3Bucket": "mapbox",
"S3Key": {
"Fn::Join": [
"",
[
"apps/streambot/",
{
"Ref": "GitSha"
},
"-example.zip"
]
]
}
}, "Role" : {
"Fn::GetAtt": [
"Role",
"Arn"
]
}, "Description" : "Streambot example function",
"Handler" : "index.streambot",
"MemorySize" : 128,
"Runtime" : "nodejs",
"Timeout" : 10
}
},This custom resource is backed by a globally-defined Lambda Function (see Streambot’s primary template). It puts the intended configuration to a record in DynamoDB, which the Lambda function can read at runtime.
"Config": {
"Type": "Custom::StreambotEnv",
"Properties": { "ServiceToken": {
"Ref": "StreambotEnvFunctionArn"
}, "FunctionName": {
"Ref": "Function"
}, "EventBucket": {
"Ref": "EventBucket"
},
"EventPrefix": {
"Ref": "EventPrefix"
}
}
},This custom resource is backed by a globally-defined Lambda function (see Streambot’s primary template). It creates an event source mapping between the Stream and the Function.
"Connector": {
"Type": "Custom::StreambotConnector",By depending on the configuration, we can make sure that stream events are not fed to the Lambda function before the runtime configuration is ready.
"DependsOn": "Config",
"Properties": { "ServiceToken": {
"Ref": "StreambotConnectorFunctionArn"
}, "FunctionRegion": {
"Ref": "AWS::Region"
}, "FunctionName": {
"Ref": "Function"
}, "StreamArn": {
"Fn::Join": [
"",
[
"arn:aws:kinesis:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":stream/",
{
"Ref": "Stream"
}
]
]
},BatchSize (max. number
of records per Lambda Invocation), StartingPosition (Stream
iterator type), and Enabled. "BatchSize": 1,
"StartingPosition": "TRIM_HORIZON"
}
}
},
"Outputs": {
"StreamName": {
"Value": {
"Ref": "Stream"
}
}
}
};