Module: Enscalator::Core::CfResources
- Included in:
- RichTemplateDSL
- Defined in:
- lib/enscalator/core/cf_resources.rb
Overview
Resources for cloudformation template dsl
Instance Method Summary collapse
-
#iam_instance_profile_with_full_access(role_name, *services) ⇒ String
IAM instance profile with full access policies to passed services.
-
#instance_vpc(name, image_id, subnet, security_groups, depends_on: [], properties: {}) ⇒ Object
Create ec2 instance in given vpc.
-
#instance_with_network(name, image_id, network_interfaces, properties: {}) ⇒ Object
Create ec2 instance with attached to it network interface.
-
#security_group(name, description, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object
Security group.
-
#security_group_vpc(name, description, vpc, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object
VPC Security group.
-
#subnet(name, vpc, cidr, availability_zone: '', depends_on: [], tags: {}) ⇒ Object
Subnet resource.
-
#vpc(name, cidr, enable_dns_support: nil, enable_dns_hostnames: nil, depends_on: [], tags: {}) ⇒ Object
VPC resource.
Instance Method Details
#iam_instance_profile_with_full_access(role_name, *services) ⇒ String
IAM instance profile with full access policies to passed services
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/enscalator/core/cf_resources.rb', line 130 def iam_instance_profile_with_full_access(role_name, *services) resource "#{role_name}Role", Type: 'AWS::IAM::Role', Properties: { AssumeRolePolicyDocument: { Statement: [ { Effect: 'Allow', Principal: { Service: ['ec2.amazonaws.com'] }, Action: ['sts:AssumeRole'] } ] }, Path: '/', Policies: [ { PolicyName: "#{role_name}Policy", PolicyDocument: { Statement: services.map do |s| { Effect: 'Allow', Action: "#{s}:*", Resource: '*' } end } } ] } resource "#{role_name}InstanceProfile", Type: 'AWS::IAM::InstanceProfile', Properties: { Path: '/', Roles: [ref("#{role_name}Role")] } ref("#{role_name}InstanceProfile") end |
#instance_vpc(name, image_id, subnet, security_groups, depends_on: [], properties: {}) ⇒ Object
Create ec2 instance in given vpc
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/enscalator/core/cf_resources.rb', line 180 def instance_vpc(name, image_id, subnet, security_groups, depends_on: [], properties: {}) fail "VPC instance #{name} can not contain non VPC SecurityGroups" if properties.include?(:SecurityGroups) if properties.include?(:NetworkInterfaces) fail "VPC instance #{name} can not contain NetworkInterfaces and subnet or security_groups" end properties[:ImageId] = image_id properties[:SubnetId] = subnet properties[:SecurityGroupIds] = security_groups if properties[:Tags] && !properties[:Tags].any? { |x| x[:Key] == 'Name' } properties[:Tags] << { Key: 'Name', Value: join('-', aws_stack_name, name) } end = { Type: 'AWS::EC2::Instance', Properties: properties } [:DependsOn] = depends_on unless depends_on.empty? resource name, name end |
#instance_with_network(name, image_id, network_interfaces, properties: {}) ⇒ Object
Create ec2 instance with attached to it network interface
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/enscalator/core/cf_resources.rb', line 207 def instance_with_network(name, image_id, network_interfaces, properties: {}) if ([:SubnetId, :SecurityGroups, :SecurityGroupIds] & properties).any? fail "Instance with NetworkInterfaces #{name} can not contain instance subnet or security_groups" end properties[:ImageId] = image_id properties[:NetworkInterfaces] = network_interfaces if properties[:Tags] && !properties[:Tags].any? { |x| x[:Key] == 'Name' } properties[:Tags] << { Key: 'Name', Value: join('-', aws_stack_name, name) } end = { Type: 'AWS::EC2::Instance', Properties: properties } resource name, name end |
#security_group(name, description, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object
Security group
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/enscalator/core/cf_resources.rb', line 67 def security_group(name, description, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) properties = { GroupDescription: description } properties[:SecurityGroupEgress] = security_group_egress unless security_group_egress.empty? properties[:SecurityGroupIngress] = security_group_ingress unless security_group_ingress.empty? unless .include?('Name') ['Name'] = join('-', aws_stack_name, name) end properties[:Tags] = () = { Type: 'AWS::EC2::SecurityGroup', Properties: properties } [:DependsOn] = depends_on unless depends_on.empty? resource name, name end |
#security_group_vpc(name, description, vpc, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) ⇒ Object
VPC Security group
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/enscalator/core/cf_resources.rb', line 99 def security_group_vpc(name, description, vpc, security_group_egress: [], security_group_ingress: [], depends_on: [], tags: {}) properties = { VpcId: vpc, GroupDescription: description } properties[:SecurityGroupEgress] = security_group_egress unless security_group_egress.empty? properties[:SecurityGroupIngress] = security_group_ingress unless security_group_ingress.empty? unless .include?('Name') ['Name'] = join('-', aws_stack_name, name) end properties[:Tags] = () = { Type: 'AWS::EC2::SecurityGroup', Properties: properties } [:DependsOn] = depends_on unless depends_on.empty? resource name, name end |
#subnet(name, vpc, cidr, availability_zone: '', depends_on: [], tags: {}) ⇒ Object
Subnet resource
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/enscalator/core/cf_resources.rb', line 39 def subnet(name, vpc, cidr, availability_zone: '', depends_on: [], tags: {}) properties = { VpcId: vpc, CidrBlock: cidr } properties[:AvailabilityZone] = availability_zone unless availability_zone.empty? unless .include?('Name') ['Name'] = join('-', aws_stack_name, name) end properties[:Tags] = () = { Type: 'AWS::EC2::Subnet', Properties: properties } [:DependsOn] = depends_on unless depends_on.empty? resource name, name end |
#vpc(name, cidr, enable_dns_support: nil, enable_dns_hostnames: nil, depends_on: [], tags: {}) ⇒ Object
VPC resource
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/enscalator/core/cf_resources.rb', line 13 def vpc(name, cidr, enable_dns_support: nil, enable_dns_hostnames: nil, depends_on: [], tags: {}) properties = { CidrBlock: cidr } properties[:EnableDnsSupport] = enable_dns_support unless enable_dns_support.nil? properties[:EnableDnsHostnames] = enable_dns_hostnames unless enable_dns_hostnames.nil? unless .include?('Name') ['Name'] = join('-', aws_stack_name, name) end properties[:Tags] = () = { Type: 'AWS::EC2::VPC', Properties: properties } [:DependsOn] = depends_on unless depends_on.empty? resource name, name end |