Skip to content

Instantly share code, notes, and snippets.

@smagch
Last active November 19, 2022 16:53
Show Gist options
  • Save smagch/4f880d7ebbccacce0dc0 to your computer and use it in GitHub Desktop.
Save smagch/4f880d7ebbccacce0dc0 to your computer and use it in GitHub Desktop.
AWS: CloudFormation with Elastic Beanstalk, Docker Go web app.

AWS: CloudFormation with Elastic Beanstalk, Docker Go web app

#!/bin/bash
set -e
# ensure param.json exists
if [ ! -f param.json ]; then
cat <<EOUSAGE
param.json file should exist.
You can copy param.example.json
EOUSAGE
exit 1
fi
find_param() {
local keyName="$1"
cat param.json | jq -r --arg keyName $keyName \
'.[] | select(.ParameterKey == $keyName) | .ParameterValue'
}
BUCKET_NAME=$(find_param S3Bucket)
KEY_NAME=$(find_param S3Key)
# upload git archive to S3
git archive --format zip HEAD | aws s3 cp - s3://${BUCKET_NAME}/${KEY_NAME}
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file://eb.json \
--region ap-northeast-1 \
--parameters file://param.json
FROM golang:1.3.3
RUN mkdir -p /go/src/app
WORKDIR /go/src/app
COPY . /go/src/app
EXPOSE 8080
RUN ["go", "install", "."]
CMD ["app"]
{
"AWSEBDockerrunVersion": "1"
}
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Docker Test",
"Parameters" : {
"Subnets": {
"Type": "CommaDelimitedList",
"Description": "Subnets"
},
"VpcId": {
"Type": "String",
"Description": "VPC Id"
},
"S3Bucket": {
"Type": "String",
"Description": "S3 Bucket name"
},
"S3Key": {
"Type": "String",
"Description": "S3 Key name"
}
},
"Resources" : {
"SampleApplication" : {
"Type" : "AWS::ElasticBeanstalk::Application",
"Properties" : {
"Description" : "AWS Elastic Beanstalk Docker Sample Application",
"ApplicationVersions" : [{
"VersionLabel" : "Initial Version",
"Description" : "Version 1.0",
"SourceBundle" : {
"S3Bucket" : {"Ref": "S3Bucket"},
"S3Key": {"Ref": "S3Key"}
}
}]
}
},
"SampleEnvironment" : {
"Type" : "AWS::ElasticBeanstalk::Environment",
"Properties" : {
"ApplicationName" : { "Ref" : "SampleApplication" },
"Description" : "AWS Elastic Beanstalk Environment running Docker Sample Application",
"SolutionStackName" : "64bit Amazon Linux 2014.09 v1.0.9 running Docker 1.2.0",
"VersionLabel" : "Initial Version",
"OptionSettings": [
{
"Namespace": "aws:autoscaling:launchconfiguration",
"OptionName": "InstanceType",
"Value": "t2.micro"
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "VPCId",
"Value": { "Ref" : "VpcId" }
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "Subnets",
"Value" : {
"Fn::Join": [",", { "Ref" : "Subnets" }]
}
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "ELBSubnets",
"Value" : {
"Fn::Select": ["0", {"Ref" : "Subnets" }]
}
}
]
}
}
},
"Outputs" : {
"URL" : {
"Description" : "The URL of the Elastic Beanstalk environment",
"Value" : { "Fn::Join" : [ "", [ "http://", { "Fn::GetAtt" : ["SampleEnvironment", "EndpointURL"] }]]}
}
}
}
package main
import (
"fmt"
"html"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
func main() {
http.HandleFunc("/", handler)
log.Println("start server")
log.Fatal(http.ListenAndServe(":8080", nil))
}
[
{
"ParameterKey": "VpcId",
"ParameterValue": "default-vpc-id"
},
{
"ParameterKey": "Subnets",
"ParameterValue": "subnets-1, subnet-2"
},
{
"ParameterKey": "S3Key",
"ParameterValue": "docker-sample.zip"
},
{
"ParameterKey": "S3Bucket",
"ParameterValue": "smagch-docker"
}
]
@er1c
Copy link

er1c commented May 25, 2019

Just wanted to give a 👍 good start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment