LoginSignup
0
1

More than 3 years have passed since last update.

Cloudformationでcognitoを作ったときにハマったメモ

Posted at

またくだらないことでハマったのでメモ。
今思えば、普通に頭悪い。

やりたいこと

  • Cloudformation使いたい
  • ALBにCognito組み合わせたい

具体的には
1. ALBを叩きに来たアクセスをCognitoに飛ばしたい
2. Cognitoの認証をパスしたら、ALBの後ろにいるEC2へforwardさせたい

とりあえずyaml書く。

ALBとCognitoの連携は、ElasticLoadBalancingV2Listenerで指定。

(中略)
  hogeALBLISTENER:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties: 
      #Certificates: 
      #  - Certificate
      DefaultActions: 
        - AuthenticateCognitoConfig: 
              UserPoolArn: "arn:aws:cognito-idp:ap-northeast-1:xxxxx:userpool/hoge"
              UserPoolClientId: "hogeid"
              UserPoolDomain: "https://hoge.auth.amazoncognito.com"
          Type: 
            - "authenticate-cognito"
            - "forward"
          TargetGroupArn: !Ref 'hogeALBTG'

      LoadBalancerArn: !Ref 'hogeALB'
      Port: 443
      Protocol: "HTTP"

…これじゃ通らない。
今見ると、書き方がひどすぎる。。。
「cognitoの認証をパスしたら、targetgroupに向けてforwardして」を1行で書いて通るわけがない。

通る書き方

(中略)
  hogeALBLISTENER:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties: 
      Certificates: 
        - CertificateArn: "arn:aws:acm:ap-northeast-1:xxxxxxxx:certificate/hogehoge"
      DefaultActions: 
        - AuthenticateCognitoConfig: 
              UserPoolArn: "arn:aws:cognito-idp:ap-northeast-1:xxxxxxx:userpool/hogehoge"
              UserPoolClientId: "hogehoge"
              UserPoolDomain: "hogehoge.auth.com"
          Order: 1
          Type: "authenticate-cognito"
        - TargetGroupArn: !Ref 'hogeALBTG'
          Order: 2
          Type: "forward"

      LoadBalancerArn: !Ref 'hogeALB'
      Port: 443
      Protocol: "HTTPS"

ちゃんと段階踏ませる書き方をすれば通る。

  1. Cognitoで認証させる → Order :1
  2. 通ったらtargetgroupへforwardさせる → Order :2

リファレンス読んで書いてたけど、あっちこっち飛ばされたらよくわからなくなる…

0
1
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
0
1