· 9 years ago · Dec 16, 2016, 03:30 PM
1AWSTemplateFormatVersion: "2010-09-09"
2Description: "AWS CloudFormation Sample Template Sample template EIP_With_Association: This template shows how to associate an Elastic IP address with an Amazon EC2 instance - you can use this same technique to associate an EC2 instance with an Elastic IP Address that is not created inside the template by replacing the EIP reference in the AWS::EC2::EIPAssoication resource type with the IP address of the external EIP"
3Parameters:
4 KeyName:
5 Type: "AWS::EC2::KeyPair::KeyName"
6 Description: "Name of an existing EC2 KeyPair to enable SSH access to the ECS instances."
7 Default: "key"
8 ImportVPCStack:
9 Type: String
10 Default: "vpc-1"
11 ImportNETStack:
12 Type: String
13 Default: "net-1"
14 FreeswitchName:
15 Type: String
16 Description: "Name of the FreeSWITCH instance"
17 Default: "FreeSWITCH Test 1"
18 InstanceType:
19 Type: String
20 Description: "EC2 instance type"
21 Default: "t2.nano"
22 AllowedValues:
23 - "t2.nano"
24 - "t2.micro"
25 - "t2.small"
26 - "t2.medium"
27 - "t2.large"
28 - "m3.medium"
29 - "m3.large"
30 - "m3.xlarge"
31 - "m3.2xlarge"
32 - "m4.large"
33 - "m4.xlarge"
34 - "m4.2xlarge"
35 - "m4.4xlarge"
36 - "m4.10xlarge"
37 - "c4.large"
38 - "c4.xlarge"
39 - "c4.2xlarge"
40 - "c4.4xlarge"
41 - "c4.8xlarge"
42 - "c3.large"
43 - "c3.xlarge"
44 - "c3.2xlarge"
45 - "c3.4xlarge"
46 - "c3.8xlarge"
47 - "r3.large"
48 - "r3.xlarge"
49 - "r3.2xlarge"
50 - "r3.4xlarge"
51 - "r3.8xlarge"
52 - "c3.4xlarge"
53 - "c3.8xlarge"
54 - "r3.large"
55 - "r3.xlarge"
56 - "r3.2xlarge"
57 - "r3.4xlarge"
58 - "i2.xlarge"
59 - "i2.2xlarge"
60 - "i2.4xlarge"
61 - "i2.8xlarge"
62 ConstraintDescription: "Please choose a valid instance type."
63 SSHLocation:
64 Description: "The IP address range that can be used to SSH to the EC2 instances"
65 Type: String
66 MinLength: 9
67 MaxLength: 18
68 Default: "72.xx.xxx.xxx/32"
69 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
70 ConstraintDescription: "must be a valid IP CIDR range of the form x.x.x.x/x."
71Mappings:
72 AWSRegionToAMI:
73 "us-east-1":
74 AMIID: "ami-15ddd102"
75 "us-east-2":
76 AMIID: "ami-71a7fd14"
77 "us-west-1":
78 AMIID: "ami-0294c262"
79 "us-west-2":
80 AMIID: "ami-8c16bcec"
81Resources:
82 InstanceRole:
83 Type: "AWS::IAM::Role"
84 Properties:
85 AssumeRolePolicyDocument:
86 Version: "2012-10-17"
87 Statement:
88 - Effect: Allow
89 Principal:
90 Service:
91 - "ec2.amazonaws.com"
92 Action:
93 - "sts:AssumeRole"
94 Path: "/"
95 RolePolicies:
96 Type: "AWS::IAM::Policy"
97 Properties:
98 PolicyName: "S3Download"
99 PolicyDocument:
100 Version: "2012-10-17"
101 Statement:
102 -
103 Effect: Allow
104 Action:
105 - "s3:GetObject"
106 Resource:
107 - "arn:aws:s3:::callifi-fs-configs/*"
108 - "arn:aws:s3:::callifi-fus-files/*"
109 Roles:
110 - !Ref "InstanceRole"
111 InstanceProfile:
112 Type: "AWS::IAM::InstanceProfile"
113 Properties:
114 Path: "/"
115 Roles:
116 - !Ref "InstanceRole"
117 SecurityGroup:
118 Type: "AWS::EC2::SecurityGroup"
119 Properties:
120 VpcId:
121 Fn::ImportValue:
122 !Sub "${ImportVPCStack}-VPCId"
123 GroupDescription: "Enable SSH access"
124 SecurityGroupIngress:
125 - IpProtocol: tcp
126 FromPort: 22
127 ToPort: 22
128 CidrIp: !Ref "SSHLocation"
129 Tags:
130 - Key: "Name"
131 Value: !Join [ "-", ["SG", {"Ref": "FreeswitchName" } ] ]
132 - Key: "CreatedBy"
133 Value: "Cloudformation"
134 EC2Instance:
135 Type: "AWS::EC2::Instance"
136 Metadata:
137 AWS::CloudFormation::Authentication:
138 S3AccessCreds:
139 Type: "S3"
140# accessKeyId:
141# Ref: "CfnKeys"
142# secretKey:
143# Fn::GetAtt:
144# - "CfnKeys"
145# - "SecretAccessKey"
146 Buckets:
147 - "callifi-fs-configs/"
148 - "callifi-fus-files/"
149 RoleName: !Ref "InstanceRole"
150 AWS::CloudFormation::Init:
151 Config:
152 Files:
153 /var/www/html2/extensions.conf:
154 Source: "https://s3.amazonaws.com/callifi-fs-configs/1.6.13/extensions.conf"
155 Mode: "000400"
156 Owner: "freeswitch"
157 Group: "freeswitch"
158 Authentication: "S3AccessCreds"
159 Sources:
160 /etc/freeswitch/test/: "https://s3.amazonaws.com/callifi-fs-configs/1.6.13/conf.tar.gz"
161 #/var/www/fusionpbx/: "https://s3.amazonaws.com/callifi-fus-files/fusion.tar"
162 Properties:
163 ImageId: !FindInMap [AWSRegionToAMI, !Ref "AWS::Region", AMIID]
164 IamInstanceProfile: !Ref "InstanceProfile"
165 InstanceType: !Ref "InstanceType"
166 KeyName: !Ref "KeyName"
167 SecurityGroupIds:
168 - !Ref "SecurityGroup"
169 SubnetId:
170 Fn::ImportValue:
171 !Sub "${ImportNETStack}-AZbPublicSubnet"
172 UserData:
173 "Fn::Base64": !Sub |
174 IPAddress=${IPAddress}
175 Tags:
176 - Key: "Name"
177 Value: !Ref "FreeswitchName"
178 - Key: "CreatedBy"
179 Value: "Cloudformation"
180 IPAddress:
181 Type: "AWS::EC2::EIP"
182 IPAssoc:
183 Type: "AWS::EC2::EIPAssociation"
184 Properties:
185 InstanceId: !Ref "EC2Instance"
186 EIP: !Ref "IPAddress"
187Outputs:
188 InstanceId:
189 Description: "InstanceId of the newly created EC2 instance"
190 Value: !Ref "EC2Instance"
191 InstanceIPAddress:
192 Description: "IP address of the newly created EC2 instance"
193 Value: !Ref "IPAddress"