LoginSignup
5
2

More than 1 year has passed since last update.

特定IP・VPC Endpointからのアクセスに限定したECRをCloudformationでデプロイする方法

Posted at

以下のように aws:sourceVpceaws:SourceIp を利用して、特定のIPやVPC Endpointからのアクセスに限定したECRを作成することができます。

ただ、下記のようなCloudformaitonのテンプレートをそのままデプロイすると以下のメッセージが発生してデプロイできません。

Resource handler returned message: "You are about to set a repository policy that will prevent you from setting another one in the future. Use the force parameter to override this exception.

Resources:
  MyRepo:
    Type: AWS::ECR::Repository
    Properties:
      RepositoryPolicyText:
        Version: "2012-10-17"
        Statement:
          - Sid: Deny
            Effect: Deny
            Principal: "*"
            Action:
              - ecr:*
            Condition:
              StringNotEquals:
                aws:sourceVpce:
                  - vpce-xxxxxxxxxx
              NotIpAddress:
                aws:SourceIp:
                  - 100.100.100.100/32

これは aws ecr set-repository-policy--force オプションに当たる設定が必要とのことです。

回避策をAWSサポートに問い合わせたところ、 aws:CalledVia = [cloudformation.amazonaws.com] のようにCloudformationからの呼び出しを許可することで事象を回避できました。

Resources:
  MyRepo:
    Type: AWS::ECR::Repository
    Properties:
      RepositoryPolicyText:
        Version: "2012-10-17"
        Statement:
          - Sid: Deny
            Effect: Deny
            Principal: "*"
            Action:
              - ecr:*
            Condition:
              StringNotEquals:
                aws:sourceVpce:
                  - vpce-xxxxxxxxxx
                aws:CalledVia:
                  - cloudformation.amazonaws.com
              NotIpAddress:
                aws:SourceIp:
                  - 100.100.100.100/32

ググっても出てこなくて時間を使ってしまったので、こちらに残しておきます。

5
2
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
5
2