Last active
January 6, 2021 14:51
-
-
Save adrianhall/aeffb62481d0a9cf59fe34fd234f7e8c to your computer and use it in GitHub Desktop.
CLoudFormation Template for AWS AppSync OpenWeatherMap API
This file contains 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
--- | |
Description: GraphQL based OpenWeatherMap API | |
Parameters: | |
APIName: | |
Type: String | |
Description: Name of the API - used to generate unique names for resources | |
MinLength: 3 | |
MaxLength: 20 | |
AllowedPattern: '^[a-zA-Z][a-zA-Z0-9_]*$' | |
APIKey: | |
Type: String | |
Description: The API Key for OpenWeatherMap | |
MinLength: 32 | |
MaxLength: 32 | |
AllowedPattern: '^[a-f0-9]*$' | |
Resources: | |
AppSyncApi: | |
Type: "AWS::AppSync::GraphQLApi" | |
Description: "The GraphQL API" | |
Properties: | |
AuthenticationType: "API_KEY" | |
Name: !Sub ${APIName} | |
AppSyncApiKey: | |
Type: "AWS::AppSync::ApiKey" | |
Properties: | |
Description: "Basic OpenWeatherMap API Key" | |
Expires: 1564608763 | |
ApiId: !GetAtt AppSyncApi.ApiId | |
AppSyncSchema: | |
Type: "AWS::AppSync::GraphQLSchema" | |
Properties: | |
ApiId: !GetAtt AppSyncApi.ApiId | |
Definition: | | |
type Query { | |
weatherByCity(city: String!): Weather | |
} | |
type Weather { | |
timestamp: Int | |
location: String | |
condition: Int | |
description: String | |
temperature: Float | |
pressure: Float | |
humidity: Float | |
wind_speed: Float | |
wind_direction: Int | |
cloud_cover: Float | |
rain_volume: Float | |
snow_volume: Float | |
} | |
schema { | |
query: Query | |
} | |
AppSyncOpenWeatherMapDataSource: | |
Type: "AWS::AppSync::DataSource" | |
Properties: | |
ApiId: !GetAtt AppSyncApi.ApiId | |
Name: openweathermap | |
Description: "The OpenWeatherMap Data Source" | |
Type: HTTP | |
HttpConfig: | |
Endpoint: "https://api.openweathermap.org" | |
AppSyncWeatherByCityQueryResolver: | |
Type: "AWS::AppSync::Resolver" | |
DependsOn: AppSyncSchema | |
Properties: | |
ApiId: !GetAtt AppSyncApi.ApiId | |
TypeName: Query | |
FieldName: weatherByCity | |
DataSourceName: !GetAtt AppSyncOpenWeatherMapDataSource.Name | |
RequestMappingTemplate: !Sub | | |
{ | |
"version": "2018-05-29", | |
"method": "GET", | |
"resourcePath": "/data/2.5/weather", | |
"params":{ | |
"query": { | |
"q": "$context.args.city", | |
"APPID": "${APIKey}" | |
}, | |
"headers": { | |
"Content-Type": "application/json" | |
} | |
} | |
} | |
ResponseMappingTemplate: | | |
#if($context.result.statusCode == 200) | |
## Success - decode the body and reconstruct the response with the schema in mind | |
#set($response = $util.parseJson($context.result.body)) | |
#set($result = { | |
"timestamp": $response.dt, | |
"location": $response.name, | |
"condition": $response.weather[0].id, | |
"description": $response.weather[0].description, | |
"temperature": $response.main.temp, | |
"pressure": $response.main.pressure, | |
"humidity": $response.main.humidity, | |
"wind_speed": $response.wind.speed, | |
"wind_direction": $response.wind.deg, | |
"cloud_cover": $response.clouds.all, | |
"rain_volume": $util.defaultIfNullOrEmpty($response.rain["3h"], 0), | |
"snow_volume": $util.defaultIfNullOrEmpty($response.rain["3h"], 0) | |
}) | |
$util.toJson($result) | |
#else | |
## Error - send the proper error message | |
$utils.appendError($ctx.result.body, $ctx.result.statusCode) | |
#end | |
Outputs: | |
GraphQLApiEndpoint: | |
Description: The URL to the GraphQL Endpoint | |
Value: !GetAtt AppSyncApi.GraphQLUrl | |
GraphQLApiId: | |
Description: The API ID of the GraphQL API | |
Value: !GetAtt AppSyncApi.ApiId | |
GraphQLApiKey: | |
Description: The API Key for the GraphQL API | |
Value: !GetAtt AppSyncApiKey.ApiKey |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment