Api Gatewayを指すようにRoute 53をセットアップする方法


12

Cloudformation構成ファイルを作成して、Webサイトを一度に作成します。これには、ラムダ関数の作成、API Gatewayの作成、S3バケットの設定、Route 53ゾーンおよびレコードの作成が含まれます。

これまでのところ:

  • Lambda関数とその役割の作成(機能)
  • API Gatewayを作成し、それを展開し、その役割(機能)
  • S3バケットとそのポリシーの作成(機能)
  • サイトのRoute 53ゾーンとDNSレコードの作成(機能)
  • API Gatewayのドメインを作成します(私が何をしているのかわかりません)

したがってdomain.com、S3バケット内のファイルを問題なく提供します。API GatewayにAWS URIを使用してhttps://trydsoonjc.execute-api.us-west-2.amazonaws.com/app/path/hereも問題なく機能します。

設定したいのはapi.domain.com、サーバーのAPIにアクセスするAPIゲートウェイを指すことです。

Route 53をAPI Gatewayに接続するにはどうすればよいですか?

現在の私のCloudformationは次のとおりです。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description" : "Website",

  "Parameters": {
    "DomainName": {
      "Type" : "String",
      "Description" : "The DNS name of an Amazon Route 53 hosted zone e.g. server.com",
      "AllowedPattern" : "(?!-)[a-zA-Z0-9-.]{1,63}(?<!-)",
      "ConstraintDescription" : "must be a valid DNS zone name."
    }
  },

  "Mappings" : {
    "RegionMap" : {
      "us-east-1" : { "S3HostedZoneId" : "Z3AQBSTGFYJSTF", "S3WebsiteEndpoint" : "s3-website-us-east-1.amazonaws.com" },
      "us-west-1" : { "S3HostedZoneId" : "Z2F56UZL2M1ACD", "S3WebsiteEndpoint" : "s3-website-us-west-1.amazonaws.com" },
      "us-west-2" : { "S3HostedZoneId" : "Z3BJ6K6RIION7M", "S3WebsiteEndpoint" : "s3-website-us-west-2.amazonaws.com" },
      "eu-west-1" : { "S3HostedZoneId" : "Z1BKCTXD74EZPE", "S3WebsiteEndpoint" : "s3-website-eu-west-1.amazonaws.com" },
      "ap-southeast-1" : { "S3HostedZoneId" : "Z3O0J2DXBE1FTB", "S3WebsiteEndpoint" : "s3-website-ap-southeast-1.amazonaws.com" },
      "ap-southeast-2" : { "S3HostedZoneId" : "Z1WCIGYICN2BYD", "S3WebsiteEndpoint" : "s3-website-ap-southeast-2.amazonaws.com" },
      "ap-northeast-1" : { "S3HostedZoneId" : "Z2M4EHUR26P7ZW", "S3WebsiteEndpoint" : "s3-website-ap-northeast-1.amazonaws.com" },
      "sa-east-1" : { "S3HostedZoneId" : "Z31GFT0UA1I2HV", "S3WebsiteEndpoint" : "s3-website-sa-east-1.amazonaws.com" }
    }
  },

  "Resources": {
    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [{
            "Effect": "Allow",
            "Principal": {
              "Service": "lambda.amazonaws.com"
            },
            "Action": [ "sts:AssumeRole" ]
          }]
        },
        "Path": "/",
        "Policies": [{
          "PolicyName": "execution",
          "PolicyDocument": {
            "Statement": [{
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
              ],
              "Resource": "*"
            }, {
              "Effect": "Allow",
              "Action": [
                "dynamodb:BatchGetItem",
                "dynamodb:CreateTable",
                "dynamodb:DeleteItem",
                "dynamodb:DescribeTable",
                "dynamodb:GetItem",
                "dynamodb:PutItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:UpdateItem",
                "s3:GetObject",
                "s3:PutObject",
                "s3:ListBucket"
              ],
              "Resource": "*"
            }]
          }
        }]
      }
    },

    "APIGatewayExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [{
            "Effect": "Allow",
            "Principal": {
              "Service": "apigateway.amazonaws.com"
            },
            "Action": [ "sts:AssumeRole" ]
          }]
        },
        "Path": "/",
        "Policies": [{
          "PolicyName": "execution",
          "PolicyDocument": {
            "Statement": [{
              "Effect": "Allow",
              "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
              ],
              "Resource": "*"
            }, {
              "Effect": "Allow",
              "Action": [
                "lambda:InvokeFunction"
              ],
              "Resource": "*"
            }]
          }
        }]
      }
    },

    "LambdaFunctionUpdate": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": "exports.handler = function (event, context) { context.succeed(\"Hello, World!\"); };"
        },
        "Description": "Update handler.",
        "Handler": "index.handler",
        "MemorySize": 128,
        "Role": { "Fn::GetAtt": ["LambdaExecutionRole", "Arn" ] },
        "Runtime": "nodejs4.3",
        "Timeout": 30
      }
    },

    "APIGateway": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Body": @@swagger,
        "FailOnWarnings": true,
        "Name": "smallPictures",
        "Description": "Structured wiki"
      }
    },

    "APITDeploymentTest": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
        "RestApiId": { "Ref": "APIGateway" },
        "Description": "Deploy for testing",
        "StageName": "smallPicturesTesting"
      }
    },

    "WebsiteBucket" : {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "BucketName": {"Ref":"DomainName"},
        "AccessControl" : "PublicRead",
        "WebsiteConfiguration" : {
          "IndexDocument" : "index.html",
          "ErrorDocument" : "404.html"
        }
      },
      "DeletionPolicy" : "Retain"
    },

    "WebsiteBucketPolicy" : {
      "Type" : "AWS::S3::BucketPolicy",
      "Properties" : {
        "Bucket" : {"Ref" : "WebsiteBucket"},
        "PolicyDocument": {
          "Statement": [{
              "Action": [ "s3:GetObject" ],
              "Effect": "Allow",
              "Resource": { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "WebsiteBucket" } , "/*" ]]},
              "Principal": "*"
          }]
        }
      }
    },

    "DNS": {
      "Type": "AWS::Route53::HostedZone",
      "Properties": {
        "HostedZoneConfig": {
          "Comment": { "Fn::Join" : ["", ["Hosted zone for ", { "Ref" : "DomainName" } ]]}
        },
        "Name": { "Ref" : "DomainName" },
        "HostedZoneTags" : [{
          "Key": "Application",
          "Value": "Blog"
        }]
      }
    },

    "DNSRecord": {
      "Type": "AWS::Route53::RecordSetGroup",
      "Properties": {
        "HostedZoneName": {
            "Fn::Join": [ "", [ { "Ref": "DomainName" }, "." ]]
        },
        "Comment": "Zone records.",
        "RecordSets": [
          {
            "Name": { "Ref": "DomainName" },
            "Type": "A",
            "AliasTarget": {
              "HostedZoneId": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "S3HostedZoneId" ]},
              "DNSName": { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "S3WebsiteEndpoint" ]}
            }
          }, {
            "Name": { "Fn::Join" : ["", ["www.", { "Ref" : "DomainName" }]]},
            "Type": "CNAME",
            "TTL" : "900",
            "ResourceRecords" : [
              {"Fn::GetAtt":["WebsiteBucket", "DomainName"]}
            ]
          }
        ]
      }
    }
  },

  "Outputs": {
    "WebsiteURL": {
      "Value": { "Fn::GetAtt": ["WebsiteBucket", "WebsiteURL" ] },
      "Description": "URL for website hosted on S3"
    }
  }
}

4
カスタムドメイン名(ホスト名)は、API Gatewayエンドポイントを直接ポイントしていません。API Gatewayがカスタムドメイン名を期待/サポートするように構成された、API Gatewayによって作成/所有/制御される、基盤となるCloudFrontディストリビューションを指します。これは、単純にRoute 53でレコードを作成するよりも複雑です。このプロセスに慣れていない場合は、API Gateway APIホスト名としてカスタムドメイン名を使用を確認する必要があります。
マイケル-sqlbot

2
API GatewayではHTTPSを有効にする必要があるため、設定の一部として、カスタムホスト名のSSL証明書を提供するか、Amazon Certificate Managerから取得する必要もあります。
マイケル-sqlbot

回答:


4

証明書マネージャーを使用してSSL証明書を作成する必要があります。エッジエンドポイントの場合、eu-east-1で作成し、リージョナルエンドポイントとプライベートエンドポイントの場合、APIゲートウェイをデプロイするリージョン(またはラムダ)で作成します。詳細はこちらをご覧ください。私はARNをCertificateArn

以下を構成する必要がありますAWS::ApiGateway::DomainName

"MyDomainName": {
  "Type": "AWS::ApiGateway::DomainName",
  "Properties": {
    "DomainName": {"Ref: "DomainName"},
    "CertificateArn": "arn:aws:acm:us-east-1:111122223333:certificate/fb1b9770-a305-495d-aefb-27e5e101ff3"
  }
}

これにより、API Gatewayのドメインが有効になります。次に、特定の展開段階でAPI(つまり、RestAPI)を公開する必要があります。テンプレートには、展開段階はありません。を見てくださいAWS::ApiGateway::Stage。最小限の例は次のようになります。

"Prod": {
            "Type": "AWS::ApiGateway::Stage",
            "Properties": {
                "StageName": "Prod",
                "Description": "Prod Stage",
                "RestApiId": {
                    "Ref": "APIGateway"
                },
                "DeploymentId": {
                    "Ref": "APITDeploymentTest"
                },
}

ただし、ほとんどの場合、追加の構成が必要になります。MethodSettings物件をご覧になることをお勧めします。

最後に、ベースパスマッピングリソースをデプロイしますAWS::ApiGateway::BasePathMapping。このように作成したステージにベースパスをマップすることをお勧めします。

"ProdDomainBasePath": {
  "Type" : "AWS::ApiGateway::BasePathMapping",
  "Properties" : {
    "DomainName" : {"Ref: "DomainName"},
    "RestApiId" : {"Ref": "APIGateway"},
    "Stage" : "Prod"
  }
}

AWS::ApiGateway::Stageリソースを変更する場合、対応するAWS::ApiGateway::Deploymentリソースを強制的に更新する必要があります-これは通常、AWS::ApiGateway::Deploymentリソースの名前を変更することを意味します。それ以外の場合は展開されません。

それはそれを行う必要があります。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.