1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CFnでWAF付きApp Runnerを作る

Posted at

はじめに

半年ほど前に、AWS App RunnerにWAFが使えるようになりました。

今回はWAFを付けたApp RunnerをCFnで作ってみました。
アプリケーションは、過去に作っているplumberのコンテナをECRにPUSHして使いました。

概要

  • App RunnerにWAFを付ける場合は、別途Type: AWS::WAFv2::WebACLAssociationで宣言する
    • Type: AWS::AppRunner::Serviceのプロパティ内ではない
  • サンプル
  WebACLAssociation:
    Type: AWS::WAFv2::WebACLAssociation
    Properties:
      WebACLArn: [WebACLのArn]
      ResourceArn: [App RunnerのServiceArn]

参考

やったこと

準備~リポジトリへPUSH

作業用インスタンスには、Cloud9のt3.smallを用いました。

コンテナにPUSHまでは過去の記事を流用します。

上記の記事から、以下のセクションをそのまま実行します。

作成

以下を行うCFnを実行します。

  • App Runner用IAMロールの作成
  • WAFの作成
    • 今回はIP許可リストとしています
  • App Runnerの作成
  • App RunnerにWAFを設定

パラメータは以下です。

  • EcrRepoUri:作成したコンテナのURI
    • バージョンも要指定
  • AllowAddresses:許可するIP群
createAppRunner.yaml
AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  EcrRepoUri:
    Type: String
  AllowAddresses:
    Type: CommaDelimitedList
    Default: "1.1.1.1/32,2.2.2.2/32"

Resources:
  RoleForAR:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - build.apprunner.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess

  WebACL:
    Type: AWS::WAFv2::WebACL
    Properties:
      Name: WebACL_AppRunner
      Scope: REGIONAL
      DefaultAction:
        Block: {}
      VisibilityConfig:
        SampledRequestsEnabled: true
        CloudWatchMetricsEnabled: true
        MetricName: WebACL_AppRunner
      Rules:
        -
          Name: rules-allow-ip
          Priority: 0
          Action:
            Allow: {}
          Statement:
            IPSetReferenceStatement:
              Arn: !GetAtt WAFIPSet.Arn
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: rules-allow-ip

  WAFIPSet:
    Type: AWS::WAFv2::IPSet
    Properties:
      Name: IPAllowLists
      Scope: REGIONAL
      IPAddressVersion: IPV4
      Addresses: !Ref AllowAddresses

  ARforPl:
    Type: AWS::AppRunner::Service
    Properties:
      ServiceName: golang-container-app
      SourceConfiguration:
        AuthenticationConfiguration:
          AccessRoleArn: !GetAtt RoleForAR.Arn
        AutoDeploymentsEnabled: true
        ImageRepository:
          ImageIdentifier: !Ref EcrRepoUri
          ImageRepositoryType: ECR
          ImageConfiguration:
            Port: 80
      InstanceConfiguration:
        Cpu: 1 vCPU
        Memory: 2 GB

  WebACLAssociation:
    Type: AWS::WAFv2::WebACLAssociation
    Properties:
      WebACLArn: !GetAtt WebACL.Arn
      ResourceArn: !GetAtt ARforPl.ServiceArn

Outputs:    
  AppRunnerServiceUrl:
    Value: !GetAtt ARforPl.ServiceUrl

以下は、コマンドで作成する例になります。IPには確認くんなどで、自分のIPをセットしてください。

REGION="ap-northeast-1"
STACKNAME="create-apprunner"
aws cloudformation create-stack \
    --stack-name ${STACKNAME} \
    --template-body file://createAppRunner.yaml \
    --region ${REGION} \
    --capabilities CAPABILITY_NAMED_IAM \
    --parameters \
        ParameterKey=EcrRepoUri,ParameterValue='123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/test-plumber-ecs:latest' \
        ParameterKey=AllowAddresses,ParameterValue='111.111.111.111/32'

確認

CFnスタックのOutputでServiceUrlを出力しています。最後に以下を付与して確認できます。

  • /hello
  • /hello/jiro/21
  • /fn?x=2
  • /plot

許可IPで制限をかけていますので、「自PCからはOK」「Cloud9からはNG」等でWAFが機能しているか確認できます。

削除

CFnで作成していますので、スタックを削除することでリソースの削除ができます。

おわりに

今回はWAFを付けたApp RunnerをCFnで作成してみました。
目的としては「特定の顧客用にIP制限をかけてAPIを提供する環境」を作りたいと考えており、App RunnerにWAFが付けられるとのことで今回試してみました。
この記事がどなたかのお役に立てれば幸いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?