From building to deploying Ruby on Jets in docker-compose environment <Part 2>

Overview

In the previous article, I wrote about creating a local environment for Ruby on Jets.

From building to deploying Ruby on Jets in docker-compose environment <Part 1>

This time I would like to summarize up to the point of actually deploying.

Prepare the user

Create an IAM user from the console and get the credentials.

https://console.aws.amazon.com/iam/home#/users

Check "Programmatic access" because it is a user only for deploying this time.

Untitled.png

To create a policy, press the tab called "JSON" and paste the json described below.

Untitled (1).png

Json for policy setting

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "apigateway:*",
                "cloudformation:*",
                "dynamodb:*",
                "events:*",
                "iam:*",
                "lambda:*",
                "logs:*",
                "route53:*",
                "s3:*"
             ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Full action allowed for convenience.

If you want to minimize your IAM policy from a security perspective

Please refer to the following page and set the policy for the IAM user for deployment.

(I set it to the minimum.)

https://rubyonjets.com/docs/extras/minimal-deploy-iam/

Although it was not listed in the official document at this time, I think that it is necessary to add the following description in order to set VPC (or rather, in the VPC setting written in application.rb The deployment has failed.)

#Added to Statement
{
    "Version": "2012-10-17",
    "Statement": [
        {
Omission
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcs"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

I won't explain it here, but the caveat is that this IAM policy is different from the IAM policy you set for the function role. You can attach an IAM policy to a function role in config/application.rb. I will write that as well when I feel like it.

Enter a name of your choice to create the policy.

Untitled (2).png

After this, create a user with your favorite settings.

Set credential locally

Go to the IAM user screen created in the IAM console and create an access key and secret key from the "Credentials" tab.

Set the obtained two keys in ~/.aws/credentials and ~/.aws/config referring to the following.

https://rubyonjets.com/docs/deploy/

~/.aws/credentials:

[default]
aws_access_key_id=<access key>
aws_secret_access_key=<Secret key>

~/.aws/config:

[default]
region=<region>
output=json

Deploy execution

Use the command jets deploy to deploy. By the way, cloud formation is running behind the scenes.

Deploy as is from the docker container.

By the way, aws-cli is in the container in the Dockerfile described in ①.

(The settings of Dockerfile and docker-compose.yml are posted in ①)

Since the environment variables of the container are managed by .env.container, ENV is described here.

# .env.container
JETS_ENV = production

Next, describe environment variables for use in the function, such as DB information, in .env.production.remote.

(.env.development is for local, .env.development.remote is for remote, as well as staging and prod)

# .env.production.remote
#Describe DB information etc.

All you have to do is deploy!

$ docker-compose run --rm app bundle exec jets deploy

If you can see the application deployed from the lambda console, you're done!

https://ap-northeast-1.console.aws.amazon.com/lambda/home?region=ap-northeast-1#/applications

Create staging environment

With jets, you can easily create another environment with the environment variable JETS_ENV described earlier.

Try setting it in .env etc. as follows

# .env.container
JETS_ENV=staging

Deploy as before

At this time, write the environment variables of the function in .env.staging.remote.

$ docker-compose run --rm app bundle exec jets deploy

If you want to change the environment, you only need to change the value of JETS_ENV, so you can also build CI/CD.

All you have to do is switch the environment variables!

Application removal

You can delete all the resources that depend on the application with the following command

Be careful when handling

$ docker-compose run --rm app bundle exec jets delete

https://rubyonjets.com/reference/jets-delete/

Summary

I still have a lot of things I want to write, but for the time being, I'm about to deploy

I was able to go! !!

I would like to continue to communicate what I noticed and what I was stuck with.

Recommended Posts

From building to deploying Ruby on Jets in docker-compose environment <Part 2>
From building to deploying Ruby on Jets in docker-compose environment <Part 1>
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Ruby on Rails --From environment construction to simple application development on WSL2
Notes on building Kotlin development environment and migrating from Java to Kotlin
[Ruby] Building a Ruby development environment on Ubuntu
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Building a Ruby environment for classes on Mac
Building OpenPose (Pytorch_Realtime_Multi-Person_Pose_Estimation) environment on Docker: training part
[Ruby on Rails] How to install Bootstrap in Rails
Build a development environment to create Ruby on Jets + React apps with Docker
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
[Ruby on Rails] How to write enum in Japanese
[Updated from time to time] Ruby on Rails Convenient methods
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
The road from Git clone to building the environment and participating in the project (all in full)
From Java to Ruby !!
[Ruby on Rails] From MySQL construction to database change
From Ruby on Rails error message display to Japanese localization
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails
[Docker context] ~ How to access docker in remote environment from VScode ~
Update Ruby in Unicorn environment
Ruby on Jets Verification (WSL)
I can't log in to MySQL from Django when using docker-compose
How to get and add data from Firebase Firestore in Ruby
Things to remember and concepts in the Ruby on Rails tutorial
Steps to build a Ruby on Rails development environment with Vagrant
How to iterate infinitely in Ruby
Try to implement Yubaba in Ruby
How to use Ruby on Rails
Ruby on Rails Japanese-English support i18n
How to install Bootstrap in Ruby
Ruby on Rails 6.0 environment construction memo
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Introduction to Ruby (from other languages)
Notes on migrating from CircleCI 1.0 to 2.0
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to develop from VScode in a remote destination environment or a remote destination container environment
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
What to do when Blocked Host: "host name" appears in Ruby on Rails
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to solve the local environment construction of Ruby on Rails (MAC)!
A story about converting character codes from UTF-8 to Shift-jis in Ruby
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development