Last active
January 12, 2019 11:27
-
-
Save franzwong/7441ebc749b66efcf59a21cc5b86b4a0 to your computer and use it in GitHub Desktop.
HowTo: Create AWS Lambda with Cloudformation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: 2010-09-09 | |
Parameters: | |
AwsRegion: | |
Type: String | |
Default: us-east-1 | |
# This bucket stores the lambda function package | |
LambdaCodeBucket: | |
Type: String | |
Default: my-lambda | |
LambdaCodeKey: | |
Type: String | |
Default: exchange-rate.zip | |
ExchangeRateURL: | |
Type: String | |
Default: https://api.exchangeratesapi.io/latest | |
# This bucket stores the JSON file generated. | |
S3Bucket: | |
Type: String | |
Default: my-output | |
S3ObjectKey: | |
Type: String | |
Default: exchange-rate.json | |
S3ObjectCacheMaxAge: | |
Type: Number | |
Default: 14400 | |
# This parameter controls trigger interval | |
# You can find the expression format in https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html | |
ExchangeRateTriggerRate: | |
Type: String | |
Default: rate(4 hours) | |
ExchangeRateTimeout: | |
Type: Number | |
Default: 90 | |
Resources: | |
ExchangeRateLambdaRole: | |
Type: AWS::IAM::Role | |
Properties: | |
RoleName: my-exchange-rate-lambda-role | |
AssumeRolePolicyDocument: | |
Statement: | |
- Action: | |
- sts:AssumeRole | |
Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Version: 2012-10-17 | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole | |
Path: / | |
Policies: | |
# This policy allows our lambda function to write to S3 bucket | |
- PolicyName: LambdaWriteS3Policy | |
PolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Action: | |
- s3:PutObject | |
Effect: Allow | |
Resource: '*' | |
ExchangeRateLambdaFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
FunctionName: my-exchange-rate | |
# Handler is file name + '.' + function name | |
Handler: index.getExchangeRate | |
Runtime: nodejs8.10 | |
Role: !GetAtt ExchangeRateLambdaRole.Arn | |
Code: | |
S3Bucket: | |
Ref: LambdaCodeBucket | |
S3Key: | |
Ref: LambdaCodeKey | |
Timeout: !Ref ExchangeRateTimeout | |
Environment: | |
Variables: | |
AwsRegion: !Ref AwsRegion | |
ExchangeRateURL: !Ref ExchangeRateURL | |
S3Bucket: !Ref S3Bucket | |
S3ObjectKey: !Ref S3ObjectKey | |
S3ObjectCacheMaxAge: !Ref S3ObjectCacheMaxAge | |
ExchangeRateLogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: | |
!Join | |
- '' | |
- - '/aws' | |
- '/lambda' | |
- '/' | |
- !Ref ExchangeRateLambdaFunction | |
RetentionInDays: 1 | |
# Scheduling rule is defined here | |
ExchangeRateSchedule: | |
Type: AWS::Events::Rule | |
Properties: | |
ScheduleExpression: !Ref ExchangeRateTriggerRate | |
State: ENABLED | |
Targets: | |
- Arn: !Sub ${ExchangeRateLambdaFunction.Arn} | |
Id: MyExchangeRateSchedule | |
# We give permission to scheduler to invoke lambda function | |
ExchangeRateSchedulePermission: | |
Type: AWS::Lambda::Permission | |
Properties: | |
Action: lambda:InvokeFunction | |
FunctionName: !Sub ${ExchangeRateLambdaFunction.Arn} | |
Principal: events.amazonaws.com | |
SourceArn: !Sub ${ExchangeRateSchedule.Arn} | |
ExchangeRateOutputBucket: | |
Type: AWS::S3::Bucket | |
Properties: | |
BucketName: !Ref S3Bucket |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment