Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Buat template AWS SAM Anda
Buat file template AWS SAM yang menentukan komponen dalam infrastruktur Anda.
Untuk membuat template AWS SAM Anda
-
Membuat sebuah direktori bernama
SAM-Tutorial
. -
Di
SAM-Tutorial
direktori Anda, buat file bernamatemplate.yml
. -
Salin kode YAML berikut ke dalam
template.yml
. Ini adalah AWS SAM template Anda.AWSTemplateFormatVersion : '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: A sample SAM template for deploying Lambda functions. Resources: # Details about the myDateTimeFunction Lambda function myDateTimeFunction: Type: AWS::Serverless::Function Properties: Handler: myDateTimeFunction.handler Runtime: nodejs18.x # Instructs your myDateTimeFunction is published to an alias named "live". AutoPublishAlias: live # Grants this function permission to call lambda:InvokeFunction Policies: - Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: '*' DeploymentPreference: # Specifies the deployment configuration Type: Linear10PercentEvery1Minute # Specifies Lambda functions for deployment lifecycle hooks Hooks: PreTraffic: !Ref beforeAllowTraffic PostTraffic: !Ref afterAllowTraffic # Specifies the BeforeAllowTraffic lifecycle hook Lambda function beforeAllowTraffic: Type: AWS::Serverless::Function Properties: Handler: beforeAllowTraffic.handler Policies: - Version: "2012-10-17" # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus Statement: - Effect: "Allow" Action: - "codedeploy:PutLifecycleEventHookExecutionStatus" Resource: !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' - Version: "2012-10-17" # Grants this function permission to call lambda:InvokeFunction Statement: - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: !Ref myDateTimeFunction.Version Runtime: nodejs18.x # Specifies the name of the Lambda hook function FunctionName: 'CodeDeployHook_beforeAllowTraffic' DeploymentPreference: Enabled: false Timeout: 5 Environment: Variables: NewVersion: !Ref myDateTimeFunction.Version # Specifies the AfterAllowTraffic lifecycle hook Lambda function afterAllowTraffic: Type: AWS::Serverless::Function Properties: Handler: afterAllowTraffic.handler Policies: - Version: "2012-10-17" Statement: # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus - Effect: "Allow" Action: - "codedeploy:PutLifecycleEventHookExecutionStatus" Resource: !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' - Version: "2012-10-17" Statement: # Grants this function permission to call lambda:InvokeFunction - Effect: "Allow" Action: - "lambda:InvokeFunction" Resource: !Ref myDateTimeFunction.Version Runtime: nodejs18.x # Specifies the name of the Lambda hook function FunctionName: 'CodeDeployHook_afterAllowTraffic' DeploymentPreference: Enabled: false Timeout: 5 Environment: Variables: NewVersion: !Ref myDateTimeFunction.Version
Template ini menentukan berikut ini. Untuk informasi selengkapnya, lihat konsep AWS SAM templat.
- Fungsi Lambda disebut
myDateTimeFunction
-
Ketika fungsi Lambda ini diterbitkan,
AutoPublishAlias
baris dalam template menautkannya ke alias bernama.live
Kemudian dalam tutorial ini, pembaruan untuk fungsi ini memicu penerapan yang secara bertahap menggeser lalu lintas produksi dari versi asli ke versi yang diperbarui. AWS CodeDeploy - Dua fungsi validasi penerapan Lambda
-
Fungsi Lambda berikut dijalankan selama kait siklus CodeDeploy hidup. Fungsi berisi kode yang memvalidasi penyebaran yang diperbarui.
myDateTimeFunction
Hasil tes validasi diteruskan CodeDeploy menggunakan metodePutLifecycleEventHookExecutionStatus
API-nya. Jika tes validasi gagal, penerapan gagal dan dibatalkan.-
CodeDeployHook_beforeAllowTraffic
berjalan selamaBeforeAllowTraffic
pengait. -
CodeDeployHook_afterAllowTraffic
berjalan selamaAfterAllowTraffic
pengait.
Nama kedua fungsi dimulai dengan
CodeDeployHook_
.CodeDeployRoleForLambda
Peran memungkinkan panggilan keinvoke
metode Lambda hanya dalam fungsi Lambda dengan nama yang dimulai dengan awalan ini. Untuk informasi selengkapnya, lihat AppSpec Bagian 'kait' untuk penerapan AWS Lambda dan PutLifecycleEventHookExecutionStatusdi Referensi CodeDeploy API. -
- Deteksi otomatis fungsi Lambda yang diperbarui
-
AutoPublishAlias
Istilah ini memberi tahu kerangka kerja untuk mendeteksi kapanmyDateTimeFunction
fungsi berubah, dan kemudian menerapkannya menggunakanlive
alias. - Konfigurasi penerapan
-
Konfigurasi deployment menentukan tingkat di mana CodeDeploy aplikasi Anda mengalihkan lalu lintas dari versi asli fungsi Lambda ke versi baru. Template ini menentukan konfigurasi deployment yang telah ditetapkan.
Linear10PercentEvery1Minute
catatan
Anda tidak dapat menentukan konfigurasi penerapan kustom dalam template AWS SAM. Untuk informasi selengkapnya, lihat Create a Deployment Configuration.
- Fungsi kait siklus hidup penerapan
-
Hooks
Bagian ini menentukan fungsi yang akan dijalankan selama kait peristiwa siklus hidup.PreTraffic
menentukan fungsi yang berjalan selamaBeforeAllowTraffic
hook.PostTraffic
menentukan fungsi yang berjalan selamaAfterAllowTraffic
hook. - Izin untuk Lambda untuk memanggil fungsi Lambda lain
-
lambda:InvokeFunction
Izin yang ditentukan memberikan peran yang digunakan oleh izin aplikasi AWS SAM untuk memanggil fungsi Lambda. Ini diperlukan saatCodeDeployHook_afterAllowTraffic
fungsiCodeDeployHook_beforeAllowTraffic
and memanggil fungsi Lambda yang diterapkan selama pengujian validasi.