1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS CloudFormationでECRリポジトリを構築しよう

1
Last updated at Posted at 2021-02-03

AWS CloudFormationでECRリポジトリを構築しようのアイキャッチ

この記事でわかること

  • AWS CloudFormationでECRリポジトリを構築しよう で扱う作業の全体像
  • 手順を進める前に確認したい前提
  • 今の環境で試すときに気をつけたい点

はじめに

AWS CloudFormationを使ってVPC構築のテンプレートのサンプルです。

テンプレートの概要が分からない場合は、はじめてのAWS CloudFormationテンプレートを理解するを参考にしてください。

コードはGitHubにもあります。

特定ブランチだけを取得する場合は、次のコマンドを実行します。

# ecr-01 ブランチだけを取得する
git clone -b ecr-01 --single-branch \
  https://github.com/s-horikoshi-public/CloudFormation-Template.git

今回は、akane というシステムの dev 環境を想定しています。
同じ構成で違う環境を作成する場合は、{環境名}-parameters.jsonを別途作成します。

ディレクトリ構成
    akane (システム)
      └── ecr (スタック)
          ├── dev-parameters.json (dev 環境のパラメータ)
          └── ecr.yml (CFnテンプレート)

AWS リソース構築内容

  1. ECRスタック
    - リポジトリ (タグのイミュータビリティ有効、イメージスキャンの設定有効)
    - ライフサイクルポリシールール (30イメージ以上は削除する)

実行環境の準備

AWS CloudFormationを動かすためのAWS CLIの設定を参考にしてください。

AWS リソース構築手順

  1. 次を実行してスタックを作成

    ./create_stacks.sh
    
  2. 次を実行してスタックを削除

    ./delete_stacks.sh
    

構築テンプレート

1. ECRスタック

repo.yml
AWSTemplateFormatVersion: 2010-09-09
Description: ECR For Akane

# Metadata:

Parameters:
  SystemName:
    Type: String
    AllowedPattern: '[a-zA-Z0-9-]*'
  EnvType:
    Description: Environment type.
    Type: String
    AllowedValues: [dev, stg, prod]
    ConstraintDescription: must specify dev, stg or prod.

Mappings:
  AzMap:
    ap-northeast-1:
      1st: ap-northeast-1a
      2nd: ap-northeast-1d
      3rd: ap-northeast-1c

# Conditions

# Transform

Resources:
  # ECR Repositoryを作成
  akaneECRRepository:
    Type: AWS::ECR::Repository
    Properties:
      ImageScanningConfiguration:
        ScanOnPush: true
      ImageTagMutability: IMMUTABLE
      LifecyclePolicy:
        LifecyclePolicyText: '{"rules":[{"rulePriority":1,"description":"Delete more than 30 images","selection":{"tagStatus":"any","countType":"imageCountMoreThan","countNumber":30},"action":{"type":"expire"}}]}'
      RepositoryName: !Sub
        - ${SystemName}-${EnvType}-repo
        - {SystemName: !Ref SystemName, EnvType: !Ref EnvType}
      Tags:
        - Key: Name
          Value: !Sub
          - ${SystemName}-${EnvType}-repo
          - {SystemName: !Ref SystemName, EnvType: !Ref EnvType}
        - Key: SystemName
          Value: !Ref SystemName
        - Key: EnvType
          Value: !Ref EnvType

Outputs:
  akaneECRRepository:
    Value: !GetAtt akaneECRRepository.Arn
    Export:
      Name: !Sub
        - ${SystemName}-${EnvType}-repo-arn
        - {SystemName: !Ref SystemName, EnvType: !Ref EnvType}
dev-parameters.json
{
    "Parameters": [
        {
            "ParameterKey": "SystemName",
            "ParameterValue": "akane"
        },
        {
            "ParameterKey": "EnvType",
            "ParameterValue": "dev"
        }
    ]
}

2. 実行ファイル

create_stacks.sh
#!/bin/sh
SYSTEM_NAME=akane
ENV_TYPE=dev

function create_stack() {
STACK_NAME=$1
aws cloudformation create-stack \
--stack-name ${SYSTEM_NAME}-${ENV_TYPE}-${STACK_NAME} \
--template-body file://./${SYSTEM_NAME}/${STACK_NAME}/${STACK_NAME}.yml \
--cli-input-json file://./${SYSTEM_NAME}/${STACK_NAME}/${ENV_TYPE}-parameters.json

aws cloudformation wait stack-create-complete \
--stack-name ${SYSTEM_NAME}-${ENV_TYPE}-${STACK_NAME}
}

create_stack ecr
delete_stacks.sh
#!/bin/sh
SYSTEM_NAME=akane
ENV_TYPE=dev

function delete_stack() {
STACK_NAME=$1
aws cloudformation delete-stack \
--stack-name ${SYSTEM_NAME}-${ENV_TYPE}-${STACK_NAME}

aws cloudformation wait stack-delete-complete \
--stack-name ${SYSTEM_NAME}-${ENV_TYPE}-${STACK_NAME}
}

delete_stack ecr

実務で使うときの確認ポイント

  • AWSアカウント、リージョン、IAM権限、料金影響を確認してから実行する。
  • CloudFormationやTerraformを使う場合は、変更差分を確認してから反映する。
  • 検証環境で動作確認し、不要なリソースは削除する。
  • サービス仕様、上限、推奨設定は変わるため、公式ドキュメントで現在の内容を確認する。
  • AWS::ECR::Repository ImageScanningConfiguration

おわりに

ここまで読んでいただきありがとうございました。
Wealthy Designでは、AWS・Webシステム開発・AI活用など、現場で使える技術を大切にしています。

会社の取り組みは、会社サイトにまとめています。

1
1
3

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?