LoginSignup
1
0

More than 1 year has passed since last update.

LPをデプロイする技術

Last updated at Posted at 2021-07-27

目的

仕事がら、短期のキャンペーンのランディングページ(LP)を構築することが多いです。AWSを使ってS3にコンテンツを置きCloudFrontで公開します。またデータベースとしてDyanamoDBを使っていてレコードを追加したりテーブルを追加したりします。ここではLPをデプロイするための簡単なCLIを紹介します。

各種AWS

ここではaws-cliはjqはインストール済としています。

CloudFront

まず既に登録されているCloudFrontの情報を取得します。無い場合は最初はがんばってコンソールから作りましょう(^^;

export AWS_PROFILE=my-profile
export AWS_DEFAULT_REGION=ap-northeast-1
export CF_ID=zzzz
aws cloudfront get-distribution-config \
--id ${CF_ID}| \
jq -r ".DistributionConfig" \
> base.json

次に今回用に書き換えたいデータをJSONで準備します。ここで書き換えたいのは「ドメイン」と「S3のパス」と「コメント」です。

cloud_front.json
{
  "cloud_front": {
    "domain": "xxxx.example.com"
    "origin_path": "/my-bucket/xxxx/public"
    "comment": "xxxx campaign"
  }
}

Rubyプログラムで書き換えます。

make.rb
require "json"
require 'securerandom'

def read_json(path)
  File.open(path) do |j|
    hash = JSON.load(j)
  end
end

base = read_json("#{__dir__}/base.json")
params = read_json(ARGV[0])

base["Aliases"]["Items"][0] = params["cloud_front"]["domain"]
base["Origins"]["Items"][0]["OriginPath"] = params["cloud_front"]["origin_path"]
base["Comment"] = params["cloud_front"]["comment"]
base["CallerReference"] = SecureRandom.uuid

puts JSON.pretty_generate(base)

デプロイします。作成後IDが取得できるので、回収しておきます。

export AWS_PROFILE=my-profile
export AWS_DEFAULT_REGION=ap-northeast-1
ruby make.rb ./cloud_front.json > cloud_front_result.json
aws cloudfront create-distribution \
--distribution-config file://cloudfront_result.json \
| jq -r '.Distribution.Id'

S3

ローカルにあるpublicディレクトリのファイルをS3なデプロイします。
またCloudFrontにキャッシュしているデータをクリアーしています。
ここで先ほど回収したIDを設定します。

export AWS_PROFILE=my-profile
export AWS_DEFAULT_REGION=ap-northeast-1
export CAMPAIGN_NAME=xxxx
export CF_ID=yyyy

# デプロイ
aws s3 sync public/ s3://my-bucket/${CAMPAIGN_NAME}/public

# キャッシュクリアー
aws cloudfront create-invalidation --distribution-id ${CF_ID} --paths "/*" | cat
aws cloudfront get-distribution --id ${CF_ID} | jq -r '.Distribution.InProgressInvalidationBatches'

DynamoDB

レコード追加

キャンペーンテーブルにレコードを追加します。キャンペーン名とキャンペーン開始時間とキャンペーン終了時間を設定します。

data_json
{
  "campaign_name": {
    "S": "xxxx"
  },
  "start_at": {
    "N": "1577804400"
  },
  "end_at": {
    "N": "1640962800"
  }
}
export AWS_PROFILE=my-profile
export AWS_DEFAULT_REGION=ap-northeast-1
aws dynamodb put-item \
--table-name campaigns \
--item file://data.json \
--return-consumed-capacity TOTAL

テーブル作成

例えば参加者を登録するテーブルを作ります。これは一度だけデプロイします。

table.json
{
  "TableName": "paticipants_xxx",
  "AttributeDefinitions": [
    {
      "AttributeName": "uuid",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "KeyType": "HASH",
      "AttributeName": "uuid"
    }
  ],
  "ProvisionedThroughput": {
    "WriteCapacityUnits": 1,
    "ReadCapacityUnits": 1
  }
}
export AWS_PROFILE=my-profile
export AWS_DEFAULT_REGION=ap-northeast-1
aws dynamodb create-table --cli-input-json file://table.json

まとめ

AWS CLIを使ってLPでよく使うコマンドなどをまとめてみました。
CloudFrontを作るところやDynamoDBのテーブルを作る作業は1度しか行いませんが、コンソールでやると意外と時間がかかります。
S3にアップロードしたり、DymanoDBのレコード修正は頻繁に行われます。これは絶対にCLIを用意しておくべきで、だいぶ作業がはかどるようになります。
シェルスクリプトとして準備しておくとよいです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0