|
AWSTemplateFormatVersion: "2010-09-09" |
|
Transform: AWS::Serverless-2016-10-31 |
|
Resources: |
|
# A Kinesis Firehose Delivery Stream that provides |
|
# real-time record ingestion and storage of records in Amazon S3. |
|
FireHoseToS3: |
|
DependsOn: |
|
- FirehoseDeliveryPolicy |
|
Type: "AWS::KinesisFirehose::DeliveryStream" |
|
Properties: |
|
ExtendedS3DestinationConfiguration: |
|
BucketARN: !Join |
|
- "" |
|
- - "arn:aws:s3:::" |
|
- !Ref DataDestinationBucket |
|
BufferingHints: |
|
IntervalInSeconds: "60" |
|
SizeInMBs: "50" |
|
CompressionFormat: UNCOMPRESSED |
|
Prefix: firehose/ |
|
RoleARN: !GetAtt FirehoseDeliveryRole.Arn |
|
|
|
# An Amazon S3 bucket where the records are stored. |
|
DataDestinationBucket: |
|
Type: "AWS::S3::Bucket" |
|
Properties: |
|
VersioningConfiguration: |
|
Status: Enabled |
|
|
|
# The IAM role for the above Firehose Delivery Stream. |
|
FirehoseDeliveryRole: |
|
Type: "AWS::IAM::Role" |
|
Properties: |
|
AssumeRolePolicyDocument: |
|
Version: 2012-10-17 |
|
Statement: |
|
- Sid: "" |
|
Effect: Allow |
|
Principal: |
|
Service: firehose.amazonaws.com |
|
Action: "sts:AssumeRole" |
|
Condition: |
|
StringEquals: |
|
"sts:ExternalId": !Ref "AWS::AccountId" |
|
|
|
# An IAM policy for the above IAM Role which gives our Delivery Stream |
|
# the ability to deliver records to the created S3 bucket. |
|
FirehoseDeliveryPolicy: |
|
Type: "AWS::IAM::Policy" |
|
DependsOn: |
|
- DataDestinationBucket |
|
Properties: |
|
PolicyName: firehose_delivery_policy |
|
PolicyDocument: |
|
Version: 2012-10-17 |
|
Statement: |
|
- Effect: Allow |
|
Action: |
|
- "s3:AbortMultipartUpload" |
|
- "s3:GetBucketLocation" |
|
- "s3:GetObject" |
|
- "s3:ListBucket" |
|
- "s3:ListBucketMultipartUploads" |
|
- "s3:PutObject" |
|
Resource: |
|
- !Join |
|
- "" |
|
- - "arn:aws:s3:::" |
|
- !Ref DataDestinationBucket |
|
- !Join |
|
- "" |
|
- - "arn:aws:s3:::" |
|
- !Ref DataDestinationBucket |
|
- "*" |
|
Roles: |
|
- !Ref FirehoseDeliveryRole |
|
|
|
# An API Gateway execution role for the below REST API. |
|
ProcessingApiRole: |
|
Type: "AWS::IAM::Role" |
|
Properties: |
|
AssumeRolePolicyDocument: |
|
Version: 2012-10-17 |
|
Statement: |
|
- Sid: "" |
|
Effect: Allow |
|
Principal: |
|
Service: apigateway.amazonaws.com |
|
Action: "sts:AssumeRole" |
|
|
|
# An IAM policy that permits our created streaming API service to integrate |
|
# with the Kinesis Firehose API PutRecord as an AWS Service Proxy. |
|
ProcessingApiPolicy: |
|
Type: "AWS::IAM::Policy" |
|
DependsOn: |
|
- ProcessingApiRole |
|
Properties: |
|
PolicyName: api_gateway_firehose_proxy_role |
|
PolicyDocument: |
|
Version: 2012-10-17 |
|
Statement: |
|
- Effect: Allow |
|
Action: |
|
- "firehose:PutRecord" |
|
Resource: !GetAtt FireHoseToS3.Arn |
|
Roles: |
|
- !Ref ProcessingApiRole |
|
|
|
# A new REST API that acts as an AWS Service proxy to the Kinesis Firehose PutRecord API. |
|
ProcessingApi: |
|
Type: AWS::Serverless::Api |
|
DependsOn: |
|
- FireHoseToS3 |
|
- ProcessingApiRole |
|
Properties: |
|
EndpointConfiguration: REGIONAL |
|
StageName: prod |
|
DefinitionBody: |
|
swagger: 2.0 |
|
info: |
|
title: |
|
Ref: AWS::StackName |
|
paths: |
|
"/": |
|
put: |
|
consumes: |
|
- "application/json" |
|
produces: |
|
- "application/json" |
|
responses: |
|
"200": |
|
statusCode: 200 |
|
x-amazon-apigateway-integration: |
|
responses: |
|
default: |
|
statusCode: 200 |
|
credentials: !GetAtt ProcessingApiRole.Arn |
|
connectionType: INTERNET |
|
httpMethod: POST |
|
type: AWS |
|
uri: |
|
!Join [ |
|
"", |
|
[ |
|
"arn:aws:apigateway:", |
|
{ "Ref": "AWS::Region" }, |
|
":firehose:action/PutRecord", |
|
], |
|
] |
|
# The below requestTemplate transforms the incoming JSON |
|
# payload into the request object structure that |
|
# the Kinesis Firehose PutRecord API requires. |
|
passthroughBehavior: WHEN_NO_TEMPLATES |
|
requestTemplates: |
|
application/json: |
|
!Join |
|
[ |
|
"", |
|
[ |
|
"{ \"DeliveryStreamName\": \"", |
|
!Ref FireHoseToS3, |
|
"\", \"Record\": {\"Data\": \"$util.base64Encode($input.json('$'))\" } }" |
|
] |
|
] |
|
requestParameters: |
|
integration.request.header.Content-Type: "'application/x-amz-json-1.1'" |
|
|
|
Outputs: |
|
StreamingApiEndpoint: |
|
Description: The endpoint for the REST API created with API Gateway |
|
Value: |
|
!Join [ |
|
"", |
|
[ |
|
"https://", |
|
!Ref "ProcessingApi", |
|
".execute-api.", |
|
!Ref "AWS::Region", |
|
".amazonaws.com/prod", |
|
], |
|
] |