使用Terraform构建AWS App Runner + CodeCommit + CodeBuild + CodePipeline的部署环境

首先

在本文中,我们介绍了使用Terraform构建AWS App Runner + CodeCommit + CodeBuild + CodePipeline部署环境的步骤。

看完以下的文章后,我受到启发想要亲自尝试一下,并且构建了这个环境。

尝试使用Terraform快速构建AWS App Runner

用Terraform构建的整体架构图

00_terraform_apprunner.png

Terraform 代码和配置

$ tree aws-terraform-app-runner-deploy
aws-terraform-app-runner-deploy
├── apprun.tf
├── cloudwatch-event.tf
├── codebuild.tf
├── codecommit.tf
├── codepipeline.tf
├── default-sample-apprun
│   ├── Dockerfile
│   ├── buildspec.yml
│   └── src
│       └── index.php
├── ecr.tf
├── iam-policy
│   ├── apprunner-trust-policy.json
│   ├── cloudwatch-event-policy.json
│   ├── cloudwatch-event-trust-policy.json
│   ├── codebuild-policy.json
│   ├── codebuild-trust-policy.json
│   ├── codepipeline-policy.json
│   └── codepipeline-trust-policy.json
├── iam-role.tf
├── output.tf
├── terraform.tf
├── terraform.tfvars-
└── variables.tf

准备之前

・安装Terraform
・安装tfenv
・为执行Terraform所需的IAM访问密钥和秘密密钥
・用于与CodeCommit建立HTTPS连接的Git认证信息

Terraform的操作验证环境

$ terraform -version
Terraform v0.15.4
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.42.0

使用 Terraform 构建 CodePipeline + CodeCommit + CodeBuild 的环境。

假设您是在Mac环境下操作,请下载Terraform的代码。

$ git clone https://github.com/okubo-t/aws-terraform-app-runner-deploy.git

到Terraform代码所在的目录。

$ cd aws-terraform-app-runner-deploy/

创建 terraform.tfvars 文件。

$ cp -p terraform.tfvars- terraform.tfvars

根据环境的需求,可以任意更改terraform.tfvars文件中的每个参数。(下面的键是示例。)

# アクセスキー
aws_access_key = "AKIAIOSFODNN7EXAMPLE"

# シークレットキー
aws_secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

# リージョン
aws_region     = "ap-northeast-1"

# Docker ファイルを Push する Codecommit のリポジトリ名
repo_name = "sample-apprun"

# Codecommit のリポジトリの説明
repo_description = "App for App Runner"

# AWS App Runner のサービス名
service_name = "sample-apprun"

使用以下命令进行Terraform的初始设置。

$ terraform init

使用以下命令来执行环境部署。

$ terraform apply

部署完成后,记下生成的 Outputs 内容。(以下数值仅为示例。)

Apply complete! Resources: 19 added, 0 changed, 0 destroyed.

Outputs:

codecommit_repository = "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/sample-apprun"
ecr_repository = "123456789.dkr.ecr.ap-northeast-1.amazonaws.com/sample-apprun"

您可以使用以下命令来检查构建的AWS环境的组件。

$ terraform state list

这样一来,CodePipeline + CodeCommit + CodeBuild 环境的建立就完成了。

执行用于 Docker 镜像构建的流水线。

执行以下命令,克隆CodeCommit的代码库。(请参考已记录的Outputs中的 codecommit_repository的值。)

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/sample-apprun

在克隆的代码库中,创建所需构建的Docker镜像文件集。
(※在这里提供的步骤中,使用了Terraform代码中的目录(default-sample-app)的Docker文件集。)

$ tree default-sample-apprun
sample-apprun
├── Dockerfile
├── buildspec.yml
└── src
    └── index.php

1 directory, 3 files
FROM php:7.4.0-apache
COPY src/ /var/www/html/
<!DOCTYPE html>
<html lang="ja">
<head>
<title>PHP Sample</title>
  </head>
  <body>
<?php echo "Hello World v1"; ?> </body>

在这里,我们将CodeBuild执行所需的buildspec.yml文件如下所示。

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - echo $AWS_DEFAULT_REGION
      - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
      - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG

将Docker文件群推送到CodeCommit仓库中。

$ git push
01_terraform_apprunner.png
02_terraform_apprunner.png

现在,Docker镜像构建流程已经执行完毕,并已成功上传到ECR。

用Terraform構建AWS App Runner服务。

使用以下命令更改App Runner服务的构建用tf文件的文件扩展名(使tf文件生效)。

$ mv apprun.tf- apprun.tf

使用以下命令构建App Runner服务。

$ terraform apply
05_terraform_apprunner.png

这样就搞定了:
将代码推送到CodeCommit,
通过Docker Image进行CodeBuild构建,
将镜像推送到ECR,
最后部署到App Runner。

管道的建设已经完成。

整理房间

使用以下命令,删除由Terraform创建的AWS环境。

$ terraform destroy

最后

不确定是否有需要,但如果能帮得上忙的话,我会很高兴。对AWS App Runner服务的未来充满期待。

bannerAds