AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Class: Aws::RailsProvisioner::CDKCodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-rails-provisioner/cdk_code_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CDKCodeBuilder

Generates CDK projects with separate stacks:

* InitStack - VPC and Fargate Cluster
* FargetStack - Fargate Service, AutoScaling, ALB
* PipelineStack - CICD CodePipline (when enabled) that:
     * Source: CodeCommit Repository when host rails applications
     * Build : CodeBuild Project that takes care of image build, tag(commit id) and push to ECR
     * Migration: CodeBuild Project that run migration inside DB subnet (optional)
     * Deploy: CodeDeploy that deploys tagged(commit id) image from ECR to Fargate service

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :cdk_dir (String)

    Directory path for generated CDK code, defaults to “cdk-sample”

  • :vpc (Hash)

    VPC configurations @see Vpc

  • :services (Hash)
  • :db_cluster (Hash)

    DB cluster configurations @see DBCluster

  • :source_path (required, String)

    path to the directory containing Rails Application source

  • :fargate (Hash)

    Fargate service configurations @example: at `aws-rails-provisioner.yml`

    fargate:
      desired_count: 5
      task:
        environment:
          RAILS_LOG_TO_STDOUT: true
    

    @see Fargate

  • :scaling (Hash)

    Scaling definitions for the Fargate service @example: at `aws-rails-provisioner.yml`

    scaling:
      max_capacity: 7
      on_cpu:
        target_util_percent: 40
      on_request:
        requests_per_target: 20000
    

    @see Scaling

  • :loadbalancer (Hash)

    Application Loadbalancer that front the Fargate service @example: at `aws-rails-provisioner.yml`

    loadbalancer:
      internet_facing: true
    

    @see LoadBalancer

  • :cicd (Hash)

    configurations for CICD that covers: source, build, database migration with code pipline @see Pipeline



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 62

def initialize(options = {})
  @cdk_dir = options[:cdk_dir] || "cdk-sample"
  @stack_prefix = _stack_prefix

  # init, vpc & fargate cluster
  @vpc = options[:vpc] || {}

  # fargate services defs
  # including related db, alb, scaling, cicd etc.
  @services = Aws::RailsProvisioner::ServiceEnumerator.new(options[:services] || {})

  # npm cdk service packages need to be installed
  @packages = Set.new
end

Instance Attribute Details

#cdk_dirString (readonly)

Returns:

  • (String)


84
85
86
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 84

def cdk_dir
  @cdk_dir
end

#packagesSet (readonly)

Returns:

  • (Set)


81
82
83
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 81

def packages
  @packages
end

#servicesEnumerable (readonly)

Returns:

  • (Enumerable)


78
79
80
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 78

def services
  @services
end

Instance Method Details

#default_stackObject



105
106
107
108
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 105

def default_stack
  # CDK init cmd default empty stack
  "#{@cdk_dir}/lib/#{@cdk_dir}-stack.ts"
end

#default_testObject



110
111
112
113
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 110

def default_test
  # CDK init cmd default test file
  "#{@cdk_dir}/test/#{@cdk_dir}.test.ts"
end

#source_filesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aws-rails-provisioner/cdk_code_builder.rb', line 90

def source_files
  Enumerator.new do |y|
    y.yield("#{@cdk_dir}/lib/#{@cdk_dir}-init-stack.ts", init_stack)
    @services.each do |svc|
      y.yield("#{@cdk_dir}/lib/#{svc.path_prefix}-fargate-stack.ts",
        svc.fargate_stack)
      y.yield("#{@cdk_dir}/lib/#{svc.path_prefix}-pipeline-stack.ts",
        svc.pipeline_stack) if svc.enable_cicd
      @packages.merge(svc.packages)
    end

    y.yield("#{@cdk_dir}/bin/#{@cdk_dir}.ts", project)
  end
end