Buat template AWS SAM Anda - AWS CodeDeploy

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
  1. Membuat sebuah direktori bernama SAM-Tutorial.

  2. Di SAM-Tutorial direktori Anda, buat file bernamatemplate.yml.

  3. Salin kode YAML berikut ke dalamtemplate.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 metode PutLifecycleEventHookExecutionStatus API-nya. Jika tes validasi gagal, penerapan gagal dan dibatalkan.

  • CodeDeployHook_beforeAllowTrafficberjalan selama BeforeAllowTraffic pengait.

  • CodeDeployHook_afterAllowTrafficberjalan selama AfterAllowTraffic pengait.

Nama kedua fungsi dimulai denganCodeDeployHook_. CodeDeployRoleForLambdaPeran memungkinkan panggilan ke invoke 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

AutoPublishAliasIstilah ini memberi tahu kerangka kerja untuk mendeteksi kapan myDateTimeFunction fungsi berubah, dan kemudian menerapkannya menggunakan live 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

HooksBagian ini menentukan fungsi yang akan dijalankan selama kait peristiwa siklus hidup. PreTrafficmenentukan fungsi yang berjalan selama BeforeAllowTraffic hook. PostTrafficmenentukan fungsi yang berjalan selama AfterAllowTraffic hook.

Izin untuk Lambda untuk memanggil fungsi Lambda lain

lambda:InvokeFunctionIzin yang ditentukan memberikan peran yang digunakan oleh izin aplikasi AWS SAM untuk memanggil fungsi Lambda. Ini diperlukan saat CodeDeployHook_afterAllowTraffic fungsi CodeDeployHook_beforeAllowTraffic and memanggil fungsi Lambda yang diterapkan selama pengujian validasi.