如何设置Route 53指向Api网关


12

我正在编写一个Cloudformation配置文件以一次性创建一个网站。这包括创建lambda函数,创建API网关,设置S3存储桶,创建Route 53区域和记录。

至今:

  • 创建Lambda函数及其角色(有效)
  • 创建API网关,它的部署及其角色(有效)
  • 创建一个S3存储桶及其策略(有效)
  • 创建站点的Route 53区域和DNS记录(有效)
  • 为API网关创建一个域(不知道我在做什么)

因此,domain.com可以毫无问题地提供S3存储桶中的文件。将AWS URI用于API网关可以正常工作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网关端点。它指向基础的CloudFront分发,该分发是 API Gateway配置为期望/支持自定义域名之后由API Gateway创建/拥有/控制的。这比简单地在Route 53中创建记录要复杂得多。除非您已经熟悉此过程,否则您将需要复审使用自定义域名作为API网关API主机名
Michael-sqlbot

2
作为配置的一部分,您还需要为自定义主机名提供SSL证书,或从Amazon Certificate Manager获取一个证书,因为API Gateway要求启用HTTPS。
Michael-sqlbot

Answers:


4

您必须使用证书管理器创建SSL证书。对于边缘终结点,在eu-east-1中创建它,对于区域终结点和私有终结点,在您要在其中部署API网关的区域(或lambda)中创建它。在这里阅读更多。我将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网关启用域。接下来,您需要在特定的部署阶段公开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资源,以便牢记这一点。否则,它将不会被部署。

那应该做。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.