Resources:
MyApiGateway:
Type: 'AWS::Serverless::Api'
Properties:
StageName: prod
DefinitionUri: 's3://mybucket/myapi-swagger-definition.yaml'
Domain:
DomainName: 'example.com'
CertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012'
BasePath: 'myapi'
其中,使用了AWS API网关的自定义域名功能,并指定了证书ARN(Amazon Resource Name)及API的基本路径。另外,还需要将Swagger定义文件上传到一个S3桶中。
aws cloudformation package --template-file sam-template.yaml --s3-bucket mybucket --output-template-file sam-output.yaml
aws cloudformation deploy --template-file sam-output.yaml --stack-name my-api-stack --capabilities CAPABILITY_IAM
其中,第一条命令用于将SAM模板打包并上传到S3,第二条命令用于创建或更新CloudFormation堆栈。最终,可以使用以下命令来测试API:
curl -v https://example.com/myapi/hello
此时,应该能够看到API成功返回了数据。