Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisrod01/d300a932d264d6488be54042aa1b360d to your computer and use it in GitHub Desktop.
Save francisrod01/d300a932d264d6488be54042aa1b360d to your computer and use it in GitHub Desktop.
Amazon Cloud Formation Template for Node.js v0.6
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "CloudFormation Sample Template for Node.js application. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
"InstanceType": {
"Description": "EC2 instance type",
"Type": "String",
"Default": "t1.micro",
"AllowedValues": ["t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.medium", "c1.xlarge"],
"ConstraintDescription": "must be a valid EC2 instance type."
},
"RootDevice": {
"Description": "EC2 root device type ('ebs' or 'instanceStore')",
"Type": "String",
"Default": "ebs",
"AllowedValues": ["ebs", "instanceStore"],
"ConstraintDescription": "must be 'ebs' or 'instanceStore."
},
"KeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the application server.",
"Type": "String"
},
"ServerPort": {
"Description": "TCP/IP port of the application server.",
"Type": "Number",
"Default": 3000,
"MinValue": 1025,
"ConstraintDescription": "must be number and greater than well-known port number (> 1024)."
}
},
"Mappings": {
"AWSRegionArch2AMI": {
"us-east-1": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
},
"us-west-2": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
},
"us-west-1": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
},
"eu-west-1": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
},
"ap-southeast-1": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
},
"ap-northeast-1": {
"ebs": "ami-e47acbe5",
"instanceStore": "ami-047bca05"
},
"sa-east-1": {
"ebs": "NOT_YET_SUPPORTED",
"instanceStore": "NOT_YET_SUPPORTED"
}
}
},
"Resources": {
"CfnUser": {
"Type": "AWS::IAM::User",
"Properties": {
"Path": "/",
"Policies": [
{
"PolicyName": "root",
"PolicyDocument": {
"Statement": [
{
"Effect": "Allow",
"Action": "cloudformation:DescribeStackResource",
"Resource": "*"
}
]
}
}
]
}
},
"HostKeys": {
"Type": "AWS::IAM::AccessKey",
"Properties": {
"UserName": {
"Ref": "CfnUser"
}
}
},
"AppServer": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"yum": {
"git" : [],
"nodejs-compat-symlinks" : [],
"npm" : []
}
}
}
}
},
"Properties": {
"ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, { "Ref": "RootDevice" } ] },
"InstanceType": { "Ref": "InstanceType" },
"SecurityGroups": [ { "Ref": "AppServerSecurityGroup" } ],
"KeyName": { "Ref": "KeyName" },
"UserData": {
"Fn::Base64": {
"Fn::Join": ["", [
"#!/bin/bash -v\n",
"yum update -y aws-cfn-bootstrap\n",
"# Install Node.js from Official RPM\n",
"# @see https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager\n",
"yum localinstall -y --nogpgcheck http://nodejs.tchol.org/repocfg/amzn1/nodejs-stable-release.noarch.rpm || error_exit 'Failed to install Node.js'\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref": "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"# Install Git and Node.js\n",
"/opt/aws/bin/cfn-init -s ", { "Ref": "AWS::StackName" }, " -r AppServer ",
" --access-key ", { "Ref": "HostKeys" },
" --secret-key ", { "Fn::GetAtt": ["HostKeys", "SecretAccessKey"] },
" --region ", { "Ref": "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"# All is well so signal success\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"AppServer setup complete\" '", { "Ref": "WaitHandle" }, "'\n"
]]
}
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "AppServer",
"Properties": {
"Handle": { "Ref": "WaitHandle" },
"Timeout": "600"
}
},
"AppServerSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "Enable HTTP access via port 3000",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": 22,
"ToPort": 22,
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": { "Ref": "ServerPort" },
"ToPort": { "Ref": "ServerPort" },
"CidrIp": "0.0.0.0/0"
}
]
}
}
},
"Outputs": {
"WebsiteURL": {
"Value": {
"Fn::Join": ["", ["http://", { "Fn::GetAtt": [ "AppServer", "PublicDnsName" ] }, ":" , { "Ref": "ServerPort" }]]
},
"Description": "Node.js Application URL"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment