stax-examples 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24633f07dfc8e723207c73df66cc4177a4d7013f6f2d41349ce1cf8baeed28ad
4
- data.tar.gz: 47dda42ed26b9555691b1ea2abdfcee2d6c42cd808cd668c1fe753f23017bf07
3
+ metadata.gz: 7b7c15dadab8652e60a5f1e02e5c7647e641ce7106102d04fefc431e002533cb
4
+ data.tar.gz: 5ec47106c462682025eea3d5e3e9164a5bcfef2c50609f4019d3b78229b22949
5
5
  SHA512:
6
- metadata.gz: 07c517ecf8372a860bd62e66e0299bcea8684fa7cce6ea5e9ba5918bd3d24314953058d63e8bff6a1f7e3e8efb478d0ea5ece0570cff2670fe22842f7de1d0d5
7
- data.tar.gz: 36a6fee26fc2421707121f4b9b45440cadc3e45184d072329312db9de295ceb20166875a55df04be8f915baea63dbe08e24fae4680e322a8680d6f29f6429cf3
6
+ metadata.gz: 1847b17bb92ada44372d98794bcd271fcc9d5d27a53ccef38cd82751cc891bbb840ccf72f2fe2debf429ebff5c043cbc1c3a775946de8ef457bd5d1bd96986be
7
+ data.tar.gz: f188684046b8fc61d5b3fad44c30a4c37817bc1c5317948bdd1ba1ca23e83b8fd019527a4429031718d7925397f51cc422062a0acef2670e507134ab7ee72d08
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stax-examples (0.0.2)
4
+ stax-examples (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Staxfile CHANGED
@@ -2,3 +2,4 @@
2
2
  stack :vpc
3
3
  stack :app
4
4
  stack :dynamo, include: %w[DynamoDB]
5
+ stack :app, import: :vpc, include: %w[Ecs Alb Logs]
@@ -1,7 +1,7 @@
1
1
  module Stax
2
2
  module Generators
3
3
  class DynamoGenerator < Base
4
- desc 'dynamo generator.'
4
+ desc 'Add dynamo table to new or existing stack'
5
5
 
6
6
  class_option :stack, type: :string, default: nil, desc: 'stack to install table'
7
7
 
@@ -54,22 +54,6 @@ module Stax
54
54
  "#{basename} generate #{command_name} TABLENAME"
55
55
  end
56
56
 
57
- # see https://www.rubydoc.info/github/wycats/thor/Thor/Actions for methods
58
-
59
- # def create_file
60
- # create_file(filename) do
61
- # content
62
- # end
63
- # end
64
-
65
- # def create_template
66
- # template(src, dest)
67
- # end
68
-
69
- # def create_dir
70
- # empty_directory(path)
71
- # end
72
-
73
57
  end
74
58
  end
75
59
  end
@@ -0,0 +1,39 @@
1
+ module Stax
2
+ module Generators
3
+ class FargateGenerator < Base
4
+ desc 'Create basic ECS Fargate service'
5
+
6
+ class_option :stack, type: :string, default: nil, desc: 'stack to install'
7
+
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ attr_accessor :stack, :vpc, :vpcid, :subnetids
11
+
12
+ def check_args
13
+ usage! unless args.empty?
14
+ end
15
+
16
+ def ask_for_options
17
+ @stack = options[:stack] || ask('stack to use or create?', default: 'app')
18
+ @vpc = ask('vpc stack to connect?', default: 'vpc')
19
+ @vpcid = ask("vpc export to import from stack #{vpc}?", default: 'VpcId')
20
+ @subnetids = ask("subnet export to import from stack #{vpc}?", default: 'SubnetIds')
21
+ end
22
+
23
+ def add_to_staxfile
24
+ append_to_file 'Staxfile', "stack :#{stack}, import: :#{vpc}, include: %w[Ecs Alb Logs]\n"
25
+ end
26
+
27
+ def add_templates
28
+ directory '.', 'cf'
29
+ end
30
+
31
+ private
32
+
33
+ def self.banner(*args)
34
+ "#{basename} generate #{command_name}"
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ description 'fargate stack'
2
+
3
+ ## VPC stack
4
+ parameter :vpc, type: :String
5
+
6
+ ## DNS domain for route53
7
+ # parameter :domain, type: :String, default: 'example.com'
8
+
9
+ include_template(
10
+ '<%= stack %>/iam_role_exec.rb',
11
+ '<%= stack %>/iam_role_task.rb',
12
+ '<%= stack %>/log_group.rb',
13
+ '<%= stack %>/security_groups.rb',
14
+ '<%= stack %>/alb.rb',
15
+ '<%= stack %>/ecs_cluster.rb',
16
+ '<%= stack %>/ecs_task.rb',
17
+ '<%= stack %>/ecs_service.rb',
18
+ '<%= stack %>/route53.rb',
19
+ )
@@ -0,0 +1,59 @@
1
+ resource :Alb, 'AWS::ElasticLoadBalancingV2::LoadBalancer', DependsOn: [:SgAlb, :SgWeb] do
2
+ subnets Fn::split(',', Fn::import_value(Fn::sub('${<%= vpc %>}-<%= subnetids %>')))
3
+ security_groups [
4
+ Fn::ref(:SgAlb),
5
+ Fn::ref(:SgWeb),
6
+ ]
7
+ tag :Name, Fn::ref('AWS::StackName')
8
+ end
9
+
10
+ resource :AlbTg, 'AWS::ElasticLoadBalancingV2::TargetGroup', DependsOn: :Alb do
11
+ port 3000 # container port to connect
12
+ protocol :HTTP
13
+ health_check_path '/status'
14
+ health_check_port 'traffic-port'
15
+ health_check_protocol :HTTP
16
+ health_check_interval_seconds 15
17
+ health_check_timeout_seconds 5
18
+ healthy_threshold_count 2
19
+ unhealthy_threshold_count 2
20
+ target_group_attributes [
21
+ { Key: 'deregistration_delay.timeout_seconds', Value: 10 }
22
+ ]
23
+ vpc_id Fn::import_value(Fn::sub('${<%= vpc %>}-<%= vpcid %>'))
24
+ target_type :ip
25
+ end
26
+
27
+ ## listen to HTTP on port 80
28
+ resource :Alb80, 'AWS::ElasticLoadBalancingV2::Listener', DependsOn: [:Alb, :AlbTg] do
29
+ load_balancer_arn Fn::ref(:Alb)
30
+ port 80
31
+ protocol :HTTP
32
+ default_actions [ {Type: :forward, TargetGroupArn: Fn::ref(:AlbTg)} ]
33
+ end
34
+
35
+ ## listen to HTTPS on port 443
36
+ # resource :Alb443, 'AWS::ElasticLoadBalancingV2::Listener', DependsOn: [:Alb, :AlbTg] do
37
+ # load_balancer_arn Fn::ref(:Alb)
38
+ # port 443
39
+ # protocol :HTTPS
40
+ # ## default cert for requests
41
+ # certificates [
42
+ # { CertificateArn: Fn::sub('arn:aws:acm:${AWS::Region}:${AWS::AccountId}:certificate/FIXME') }
43
+ # ]
44
+ # ssl_policy 'ELBSecurityPolicy-TLS-1-2-2017-01'
45
+ # default_actions [ {Type: :forward, TargetGroupArn: Fn::ref(:AlbTg)} ]
46
+ # end
47
+
48
+ ## add extra certs
49
+ # resource :AlbCert, 'AWS::ElasticLoadBalancingV2::ListenerCertificate', DependsOn: :Alb443 do
50
+ # listener_arn Fn::ref(:Alb443)
51
+ # certificates [
52
+ # { CertificateArn: Fn::sub('arn:aws:acm:${AWS::Region}:${AWS::AccountId}:certificate/FIXME') }
53
+ # ]
54
+ # end
55
+
56
+ output :AlbArn, Fn::ref(:Alb), export: Fn::sub('${AWS::StackName}-AlbArn')
57
+ output :AlbName, Fn::get_att(:Alb, :LoadBalancerName)
58
+ output :AlbDnsName, Fn::get_att(:Alb, :DNSName)
59
+ output :AlbHostedZone, Fn::get_att(:Alb, :CanonicalHostedZoneID)
@@ -0,0 +1,5 @@
1
+ resource :EcsCluster, 'AWS::ECS::Cluster' do
2
+ cluster_name Fn::ref('AWS::StackName')
3
+ end
4
+
5
+ output :EcsCluster, Fn::ref(:EcsCluster), export: Fn::sub('${AWS::StackName}-EcsCluster')
@@ -0,0 +1,31 @@
1
+ ## cfer way to check if stack exists
2
+ @stack_exists = !client.stack_cache(client.name).empty?
3
+
4
+ resource :EcsService, 'AWS::ECS::Service', DependsOn: [:EcsCluster, :EcsTask, :SgWeb, :AlbTg] do
5
+ cluster Fn::ref(:EcsCluster)
6
+ deployment_configuration do
7
+ minimum_healthy_percent 75
8
+ maximum_percent 200
9
+ end
10
+ desired_count(0) unless @stack_exists # 0 on create, preserve value on update
11
+ launch_type :FARGATE
12
+ network_configuration do
13
+ awsvpc_configuration do
14
+ subnets Fn::split(',', Fn::import_value(Fn::sub('${<%= vpc %>}-<%= subnetids %>')))
15
+ assign_public_ip :ENABLED
16
+ security_groups [
17
+ Fn::ref(:SgWeb)
18
+ ]
19
+ end
20
+ end
21
+ task_definition Fn::ref(:EcsTask)
22
+ load_balancers [
23
+ {
24
+ ContainerName: :app,
25
+ ContainerPort: 3000,
26
+ TargetGroupArn: Fn::ref(:AlbTg),
27
+ }
28
+ ]
29
+ end
30
+
31
+ output :EcsService, Fn::ref(:EcsService), export: Fn::sub('${AWS::StackName}-EcsService')
@@ -0,0 +1,31 @@
1
+ resource :EcsTask, 'AWS::ECS::TaskDefinition', DependsOn: [:IamRoleExec, :IamRoleTask, :LogGroup] do
2
+ cpu 512 # 256 512 1024 2048 4096
3
+ memory '1GB' # 2-8 times cpu
4
+ requires_compatibilities [:FARGATE]
5
+ execution_role_arn Fn::get_att(:IamRoleExec, :Arn)
6
+ task_role_arn Fn::get_att(:IamRoleTask, :Arn)
7
+ network_mode :awsvpc
8
+ container_definitions [
9
+ {
10
+ Name: :app,
11
+ Image: Fn::sub('${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/app:latest'), # FIXME
12
+ MemoryReservation: 512,
13
+ # Environment: [
14
+ # { Name: :STACK_NAME, Value: Fn::ref('AWS::StackName') },
15
+ # ],
16
+ PortMappings: [
17
+ { ContainerPort: 3000 }
18
+ ],
19
+ LogConfiguration: {
20
+ LogDriver: :awslogs,
21
+ Options: {
22
+ 'awslogs-group' => Fn::ref(:LogGroup),
23
+ 'awslogs-region' => Fn::ref('AWS::Region'),
24
+ 'awslogs-stream-prefix' => Fn::ref('AWS::StackName'),
25
+ }
26
+ }
27
+ }
28
+ ]
29
+ end
30
+
31
+ output :EcsTask, Fn::ref(:EcsTask)
@@ -0,0 +1,19 @@
1
+ ## permissions needed for ECS agent on Fargate to pull image and exec container
2
+ resource :IamRoleExec, 'AWS::IAM::Role' do
3
+ path '/'
4
+ assume_role_policy_document(
5
+ Version: '2012-10-17',
6
+ Statement: [
7
+ {
8
+ Effect: :Allow,
9
+ Principal: {
10
+ Service: 'ecs-tasks.amazonaws.com'
11
+ },
12
+ Action: 'sts:AssumeRole'
13
+ }
14
+ ]
15
+ )
16
+ managed_policy_arns [
17
+ 'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
18
+ ]
19
+ end
@@ -0,0 +1,18 @@
1
+ ## permissions needed for running container
2
+ resource :IamRoleTask, 'AWS::IAM::Role' do
3
+ path '/'
4
+ assume_role_policy_document(
5
+ Version: '2012-10-17',
6
+ Statement: [
7
+ {
8
+ Effect: :Allow,
9
+ Principal: {
10
+ Service: 'ecs-tasks.amazonaws.com'
11
+ },
12
+ Action: 'sts:AssumeRole'
13
+ }
14
+ ]
15
+ )
16
+ # managed_policy_arns []
17
+ # policies []
18
+ end
@@ -0,0 +1,7 @@
1
+ ## destination for container logs
2
+ resource :LogGroup, 'AWS::Logs::LogGroup' do
3
+ log_group_name Fn::sub('/${AWS::StackName}')
4
+ # retention_in_days 90
5
+ end
6
+
7
+ output :LogGroup, Fn::ref(:LogGroup)
@@ -0,0 +1,16 @@
1
+ # resource :R53, 'AWS::Route53::RecordSetGroup' do
2
+ # hosted_zone_name Fn::sub('${domain}.')
3
+ # comment Fn::ref('AWS::StackName')
4
+ # record_sets [
5
+ # {
6
+ # Name: Fn::sub('${AWS::StackName}.${domain}'),
7
+ # Type: :A,
8
+ # AliasTarget: {
9
+ # HostedZoneId: Fn::get_att(:Alb, :CanonicalHostedZoneID),
10
+ # DNSName: Fn::get_att(:Alb, :DNSName),
11
+ # }
12
+ # }
13
+ # ]
14
+ # end
15
+
16
+ # output :DnsRecord, Fn::sub('${AWS::StackName}.${domain}')
@@ -0,0 +1,32 @@
1
+ ## security group for http from internet to ALB
2
+ resource :SgAlb, 'AWS::EC2::SecurityGroup' do
3
+ group_description 'HTTP access to ALB from internet'
4
+ vpc_id Fn::import_value(Fn::sub('${<%= vpc %>}-<%= vpcid %>'))
5
+ security_group_ingress [
6
+ { CidrIp: '0.0.0.0/0', IpProtocol: :tcp, FromPort: 80, ToPort: 80 },
7
+ { CidrIp: '0.0.0.0/0', IpProtocol: :tcp, FromPort: 443, ToPort: 443 },
8
+ ]
9
+ security_group_egress [
10
+ { CidrIp: '0.0.0.0/0', IpProtocol: '-1', FromPort: 0, ToPort: 0 }
11
+ ]
12
+ tag :Name, Fn::ref('AWS::StackName')
13
+ end
14
+
15
+ ## security group for http from ALB to containers
16
+ resource :SgWeb, 'AWS::EC2::SecurityGroup' do
17
+ group_description 'HTTP access from ALB to containers'
18
+ vpc_id Fn::import_value(Fn::sub('${<%= vpc %>}-<%= vpcid %>'))
19
+ security_group_egress [
20
+ { CidrIp: '0.0.0.0/0', IpProtocol: '-1', FromPort: 0, ToPort: 0 }
21
+ ]
22
+ tag :Name, Fn::ref('AWS::StackName')
23
+ end
24
+
25
+ ## separate resource so we can point sg to itself
26
+ resource :SgWebIngress, 'AWS::EC2::SecurityGroupIngress', DependsOn: :SgWeb do
27
+ group_id Fn::ref(:SgWeb)
28
+ ip_protocol :tcp
29
+ from_port 0
30
+ to_port 65535
31
+ source_security_group_id Fn::ref(:SgWeb)
32
+ end
@@ -1,5 +1,5 @@
1
1
  module Stax
2
2
  module Examples
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stax-examples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-07 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,17 @@ files:
82
82
  - Staxfile
83
83
  - lib/generators/dynamo/dynamo_generator.rb
84
84
  - lib/generators/dynamo/templates/dyn.rb.tt
85
+ - lib/generators/fargate/fargate_generator.rb
86
+ - lib/generators/fargate/templates/%stack%.rb.tt
87
+ - lib/generators/fargate/templates/%stack%/alb.rb.tt
88
+ - lib/generators/fargate/templates/%stack%/ecs_cluster.rb.tt
89
+ - lib/generators/fargate/templates/%stack%/ecs_service.rb.tt
90
+ - lib/generators/fargate/templates/%stack%/ecs_task.rb.tt
91
+ - lib/generators/fargate/templates/%stack%/iam_role_exec.rb.tt
92
+ - lib/generators/fargate/templates/%stack%/iam_role_task.rb.tt
93
+ - lib/generators/fargate/templates/%stack%/log_group.rb.tt
94
+ - lib/generators/fargate/templates/%stack%/route53.rb.tt
95
+ - lib/generators/fargate/templates/%stack%/security_groups.rb.tt
85
96
  - lib/generators/vpc/templates/%stack_name%.rb.tt
86
97
  - lib/generators/vpc/templates/%stack_name%/endpoints.rb
87
98
  - lib/generators/vpc/templates/%stack_name%/subnets.rb.tt