Skip to content

Instantly share code, notes, and snippets.

@phm200
Last active January 22, 2018 20:25
Show Gist options
  • Save phm200/07c2e220ca3e8d747be7645aaf2b7c64 to your computer and use it in GitHub Desktop.
Save phm200/07c2e220ca3e8d747be7645aaf2b7c64 to your computer and use it in GitHub Desktop.
CloudFormation Sample Template
AWSTemplateFormatVersion: "2010-09-09"
Description: "CloudFormation sample template with Lambda function for OpenWeatherMap API"
Parameters:
# Parameters let users input custom values into template when creating a stack
OpenWeatherMapApiKey:
Type: "String"
Description: "The API key for your OpenWeatherMap account, i.e. 8b2b9..."
TriggerRateMinutes:
Type: "Number"
Description: "How often the Lambda functions are invoked to keep them warm"
Default: 30
Resources:
# IAM role to for a Lambda to execute itself
executeOwnLambdaIAMRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
# Node.js Lambda function that converts a city name to an OpenWeatherMap API city id
getOpenWeatherMapCityIdLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Description: "Gets the OpenWeatherMap API city id for a city"
Code:
ZipFile: !Sub |
exports.handler = (event, context, callback) => {
const cityName = event.cityName;
console.log(
"Looking up city id for " + cityName
);
//hardcoded for sample purposes;
var cityId = 4887398; //Chicago
callback(null, { cityName: cityName, cityId: cityId});
}
Handler: "index.handler"
Role: !GetAtt
- "executeOwnLambdaIAMRole"
- "Arn"
Runtime: "nodejs6.10"
Timeout: 8
MemorySize: 128
DependsOn:
- "executeOwnLambdaIAMRole"
# IAM role to for a Lambda to execute itself and also execute the getOpenWeatherMapCityId
executeOwnAndGetCityIdLambdaIAMRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
-
PolicyName: "invokeCityIdLambda"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "lambda:InvokeFunction"
Resource:
- !GetAtt
- "getOpenWeatherMapCityIdLambdaFunction"
- "Arn"
# Node.js Lambda function that gets current weather information from OpenWeatherMap API based on city
getCurrentWeatherForCityLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Description: "Looks up current weather conditions in a city from OpenWeatherMap API"
Code:
ZipFile: !Sub |
const https = require("https");
const aws = require("aws-sdk");
const openWeatherMapApiKey = process.env["OpenWeatherMapApiKey"];
exports.handler = (event, context, callback) => {
const cityName = event.Details.ContactData.Attributes.CityName;
var lambda = new aws.Lambda({
region: "${AWS::Region}"
});
var payloadObject = { cityName: event.Details.ContactData.Attributes.CityName };
lambda.invoke(
{
FunctionName: "${getOpenWeatherMapCityIdLambdaFunction}",
Payload: JSON.stringify(payloadObject)
},
function(error, data) {
if (error) {
console.error("Failed to invoke Lambda to get city id", error);
callback("Failed to invoke Lambda to get city id: " + error);
}
if (data) {
var cityId = JSON.parse(data.Payload).cityId;
console.log("Got city id " + cityId + " for city " + cityName);
//make open weather api call
const queryString = "/data/2.5/weather?id=" + cityId + "&appid=" + openWeatherMapApiKey;
console.log("Querying API for weather with query string of " + queryString);
//hardcoded for sample purposes
callback(null, { name: "Chicago", weather: "Mist" });
}
}
);
}
Handler: "index.handler"
Role: !GetAtt
- "executeOwnAndGetCityIdLambdaIAMRole"
- "Arn"
Runtime: "nodejs6.10"
Timeout: 8
MemorySize: 128
Environment:
Variables:
OpenWeatherMapApiKey: !Sub ${OpenWeatherMapApiKey}
DependsOn:
- "executeOwnAndGetCityIdLambdaIAMRole"
# permission so that connect can invoke it
getCurrentWeatherForCityLambdaFunctionInvokePermission:
Type: AWS::Lambda::Permission
DependsOn: getCurrentWeatherForCityLambdaFunction
Properties:
FunctionName:
Ref: getCurrentWeatherForCityLambdaFunction
Action: lambda:InvokeFunction
Principal: connect.amazonaws.com
SourceAccount:
Ref: AWS::AccountId
# CloudWatch Event to periodically trigger the sample Lambda functions
sampleLambdaFunctionsTrigger:
Type: 'AWS::Events::Rule'
Properties:
ScheduleExpression: !Sub rate(${TriggerRateMinutes} minutes)
State: 'ENABLED'
Targets:
-
Arn:
Fn::GetAtt:
- "getOpenWeatherMapCityIdLambdaFunction"
- "Arn"
Input: '{ "Name": "CloudWatchEvent", "Type": "KeepWarm"}'
Id: "getOpenWeatherMapCityIdLambdaFunctionTarget1"
-
Arn:
Fn::GetAtt:
- "getCurrentWeatherForCityLambdaFunction"
- "Arn"
Input: '{ "Name": "CloudWatchEvent", "Type": "KeepWarm"}'
Id: "getCurrentWeatherForCityLambdaFunctionTarget1"
# Permissions for the CloudWatch Event to periodically trigger the getOpenWeatherMapCityId Lambda function
getOpenWeatherMapCityIdLambdaFunctionTriggerInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
Ref: "getOpenWeatherMapCityIdLambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "sampleLambdaFunctionsTrigger"
- "Arn"
# Permissions for the CloudWatch Event to periodically trigger the getCurrentWeatherForCity Lambda function
getCurrentWeatherForCityLambdaFunctionTriggerInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
Ref: "getCurrentWeatherForCityLambdaFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "sampleLambdaFunctionsTrigger"
- "Arn"
Outputs:
getOpenWeatherMapCityIdLambdaARN:
Description: "ARN of the getOpenWeatherMapCityId Lambda function"
Value:
Fn::GetAtt:
- "getOpenWeatherMapCityIdLambdaFunction"
- "Arn"
getCurrentWeatherForCityLambdaARN:
Description: "ARN of the getCurrentWeatherForCity Lambda function"
Value:
Fn::GetAtt:
- "getCurrentWeatherForCityLambdaFunction"
- "Arn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment