這是新的 CloudFormation 範本參考指南。請更新您的書籤和連結。如需 CloudFormation 入門的說明,請參閱 AWS CloudFormation 使用者指南。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS::Serverless 轉換
本主題說明如何使用 AWS::Serverless 轉換來處理採用 AWS Serverless Application Model (AWS SAM) 語法編寫的範本,並將其轉換為相容的 CloudFormation 範本。
如需有關使用 AWS::Serverless 轉換的詳細資訊,請參閱 GitHub 上的 AWS SAM 轉換
用量
若要使用 AWS::Serverless 轉換,必須在 CloudFormation 範本的頂層宣告轉換。您無法將 AWS::Serverless 作為內嵌於任何其他範本區段的轉換使用。
宣告必須使用常值字串 AWS::Serverless-2016-10-31 作為其值。您不能使用參數或函數來指定轉換值。
語法
若要在您的 CloudFormation 範本中宣告此轉換,請使用下列語法:
JSON
{ "Transform":"AWS::Serverless-2016-10-31", "Resources":{...} }
YAML
Transform: AWS::Serverless-2016-10-31 Resources:...
AWS::Serverless 轉換是沒有其他參數的獨立宣告。
範例
下列範例顯示如何使用 AWS::Serverless 轉換和 AWS SAM 語法來簡化 Lambda 函式及其執行角色的宣告。
JSON
{ "Transform":"AWS::Serverless-2016-10-31", "Resources":{ "MyFunction":{ "Type":"AWS::Serverless::Function", "Properties":{ "Handler":"index.handler", "Runtime":"nodejs20.x", "CodeUri":"s3://amzn-s3-demo-bucket/MySourceCode.zip" } } } }
YAML
Transform: AWS::Serverless-2016-10-31 Resources: MyFunction: Type: AWS::Serverless::Function Properties: Handler:index.handlerRuntime:nodejs20.xCodeUri: 's3://amzn-s3-demo-bucket/MySourceCode.zip'
從範本建立變更集時,CloudFormation 會依轉換的定義來擴充 AWS SAM 語法。處理過的範本會擴充 AWS::Serverless::Function 資源,並宣告 Lambda 函式和執行角色。
{ "Resources": { "MyFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Handler": "index.handler", "Code": { "S3Bucket": "amzn-s3-demo-bucket", "S3Key": "MySourceCode.zip" }, "Role": { "Fn::GetAtt": ["MyFunctionRole", "Arn"] }, "Runtime": "nodejs20.x" } }, "MyFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Action": ["sts:AssumeRole"], "Effect": "Allow", "Principal": { "Service": ["lambda.amazonaws.com"] } }] } } } } }
搭配使用 AWS::Serverless 與 AWS::LanguageExtensions
同時使用 AWS::Serverless 和 AWS::LanguageExtensions 轉換時,當階段名稱以非 NoEcho 參數值進行傳遞時,參考 AWS::ApiGateway::Stage 等資源需要特殊語法。
使用 Fn::Sub 產生邏輯 ID 參考,而不是將 AWS SAM 語法用於參考 ()。例如 MyApi.Stage"Ref": {"Fn::Sub":
。這會在執行時期建置正確的邏輯 ID。"${${MyApi}StageName}Stage"}
這種特殊格式的原因是因為這兩個轉換處理值的方式不同:
-
AWS::LanguageExtensions會將內建函數解析為其實際值。 -
AWS::Serverless會根據其收到靜態值還是內建函數,來建立不同的邏輯 ID。
相關資源
如需有關無伺服器應用程式和 AWS Serverless Application Model (AWS SAM) 的詳細資訊,請參閱《https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.htmlAWS Serverless Application Model 開發人員指南》。
如需 AWS SAM 特有的資源和屬性類型,請參閱《AWS Serverless Application Model 開發人員指南》中的 AWS SAM 資源和屬性。
如需有關使用巨集的一般資訊,請參閱《AWS CloudFormation 使用者指南》中的使用範本巨集在 CloudFormation 範本上執行自訂處理。