Last active
May 22, 2019 15:38
-
-
Save haranjackson/6f08d547c99c3ad9f0abf99949c0f646 to your computer and use it in GitHub Desktop.
An AWS CloudFormation template for creating an API using API Gateway, with an EC2 backend. As an example, API Gateway's /api_endpoint points to the EC2's /ec2_endpoint.
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
AWSTemplateFormatVersion: 2010-09-09 | |
Transform: AWS::Serverless-2016-10-31 | |
Parameters: | |
VpcId: | |
Type: String | |
Description: The ID of the VPC containing the EC2 instance | |
InstanceAZ: | |
Type: String | |
Description: The availability zone containing the EC2 instance | |
InstanceId: | |
Type: String | |
Description: The ID of the EC2 instance | |
InstancePort: | |
Type: String | |
Description: The port on which the EC2 instance is listening | |
Subnet: | |
Type: String | |
Description: The ID of the subnet containing the EC2 instance | |
Resources: | |
############################# | |
# VPC | |
############################# | |
TargetGroup: | |
Type: AWS::ElasticLoadBalancingV2::TargetGroup | |
Properties: | |
Port: 80 | |
Protocol: TCP | |
TargetType: instance | |
Targets: | |
- Id: !Ref InstanceId | |
Port: !Ref InstancePort | |
VpcId: !Ref VpcId | |
LoadBalancer: | |
Type: AWS::ElasticLoadBalancingV2::LoadBalancer | |
Properties: | |
Scheme: internal | |
Subnets: | |
- !Ref Subnet | |
Type: network | |
Listener: | |
Type: AWS::ElasticLoadBalancingV2::Listener | |
Properties: | |
DefaultActions: | |
- TargetGroupArn: !Ref TargetGroup | |
Type: forward | |
LoadBalancerArn: !Ref LoadBalancer | |
Port: 80 | |
Protocol: TCP | |
VpcLink: | |
Type: AWS::ApiGateway::VpcLink | |
Properties: | |
Name: vpc-link | |
TargetArns: | |
- !Ref LoadBalancer | |
############################# | |
# API | |
############################# | |
Api: | |
Type: AWS::Serverless::Api | |
Properties: | |
StageName: production | |
Cors: | |
AllowMethods: "'GET,POST,OPTIONS'" | |
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" | |
AllowOrigin: "'*'" | |
DefinitionBody: | |
swagger: 2.0 | |
paths: | |
/api_endpoint: | |
get: | |
x-amazon-apigateway-integration: | |
connectionId: !Ref VpcLink | |
connectionType: VPC_LINK | |
httpMethod: GET | |
type: http_proxy | |
uri: !Join ["", ["http://", !GetAtt LoadBalancer.DNSName, "/ec2_endpoint"]] |
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
REGION= # Enter a region, e.g. us-east-1 | |
STACK= # Enter a name for this stack | |
BUCKET= # Enter a name for an S3 bucket to use to deploy this stack | |
VPC_ID= # Enter ID of the VPC containing the EC2 instance | |
INSTANCE_AZ = # Enter Availability zone containing the EC2 instance | |
INSTANCE_ID = # Enter ID of the EC2 instance | |
INSTANCE_PORT= # Enter the port on which the EC2 instance is listening | |
SUBNET= # Enter the ID of the subnet containing the EC2 instance | |
aws s3 mb s3://$BUCKET --region $REGION | |
aws cloudformation package --template-file apigateway_with_ec2.yaml \ | |
--output-template-file package.yaml \ | |
--s3-bucket $BUCKET | |
aws cloudformation deploy --template-file package.yaml \ | |
--stack-name $STACK \ | |
--region $REGION \ | |
--capabilities CAPABILITY_NAMED_IAM \ | |
--parameter-overrides \ | |
VpcId=$VPC_ID \ | |
InstanceAZ=$INSTANCE_AZ \ | |
InstanceId=$INSTANCE_ID \ | |
InstancePort=$INSTANCE_PORT \ | |
Subnet=$SUBNET | |
rm package.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment