將應用程式規格檔案新增至修訂 CodeDeploy - AWS CodeDeploy

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將應用程式規格檔案新增至修訂 CodeDeploy

本主題說明如何將 AppSpec 檔案加入至您的部署。它還包括用於為 AWS Lambda 和 EC2 /內部部署建立 AppSpec 檔案的範本。

為 Amazon ECS 部署添加 AppSpec 文件

對於部署到 Amazon ECS 運算平台:

  • 該 AppSpec 檔案指定用於部署的 Amazon ECS 任務定義、用於路由流量的容器名稱和連接埠對應,以及在部署生命週期事件之後執行的選用 Lambda 函數。

  • 修訂與 AppSpec 檔案相同。

  • 一個 AppSpec 文件可以使用 JSON 或 YAML 來寫入。

  • 建立 AppSpec 部署時,檔案可以儲存為文字檔或直接輸入主控台。如需詳細資訊,請參閱 建立 Amazon ECS 運算平台部署 (主控台)

建立 AppSpec 檔案的步驟

  1. 將 JSON 或 YAML 範本複製到文字編輯器或主控台的 AppSpec 編輯器中。

  2. 可視需要修改範本。

  3. 使用 JSON 或 YAML 驗證程式來驗證您的 AppSpec 檔案。如果使用 AppSpec編輯器,則在選擇「建立部署」時驗證檔案。

  4. 如果您是使用文字編輯器,請儲存該檔案。如果您使 AWS CLI 用建立部署,請參考檔案 (如果 AppSpec 檔案位於您的硬碟或 Amazon S3 儲存貯體中)。如果您使用主控台,則必須將 AppSpec 檔案推送到 Amazon S3。

具有指示的 Amazon ECS 部署的 YAML AppSpec 檔案範本

以下是 Amazon ECS 部署 AppSpec 檔案的 YAML 範本,其中包含所有可用選項。如需在 hooks 區段中使用之生命週期事件的相關資訊,請參閱AppSpec Amazon ECS 部署的「掛鉤」部分

# This is an appspec.yml template file for use with an Amazon ECS deployment in CodeDeploy. # The lines in this template that start with the hashtag are # comments that can be safely left in the file or # ignored. # For help completing this file, see the "AppSpec File Reference" in the # "CodeDeploy User Guide" at # https://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html version: 0.0 # In the Resources section, you must specify the following: the Amazon ECS service, task definition name, # and the name and port of the load balancer to route traffic, # target version, and (optional) the current version of your AWS Lambda function. Resources: - TargetService: Type: AWS::ECS::Service Properties: TaskDefinition: "" # Specify the ARN of your task definition (arn:aws:ecs:region:account-id:task-definition/task-definition-family-name:task-definition-revision-number) LoadBalancerInfo: ContainerName: "" # Specify the name of your Amazon ECS application's container ContainerPort: "" # Specify the port for your container where traffic reroutes # Optional properties PlatformVersion: "" # Specify the version of your Amazon ECS Service NetworkConfiguration: AwsvpcConfiguration: Subnets: ["",""] # Specify one or more comma-separated subnets in your Amazon ECS service SecurityGroups: ["",""] # Specify one or more comma-separated security groups in your Amazon ECS service AssignPublicIp: "" # Specify "ENABLED" or "DISABLED" # (Optional) In the Hooks section, specify a validation Lambda function to run during # a lifecycle event. Hooks: # Hooks for Amazon ECS deployments are: - BeforeInstall: "" # Specify a Lambda function name or ARN - AfterInstall: "" # Specify a Lambda function name or ARN - AfterAllowTestTraffic: "" # Specify a Lambda function name or ARN - BeforeAllowTraffic: "" # Specify a Lambda function name or ARN - AfterAllowTraffic: "" # Specify a Lambda function name or ARN

Amazon ECS 部署範本的 JSON AppSpec 檔案

以下是 Amazon ECS 部署 AppSpec 檔案的 JSON 範本,其中包含所有可用選項。如需範本指示,請參閱上一節中 YAML 版本的註釋。如需在 hooks 區段中使用之生命週期事件的相關資訊,請參閱AppSpec Amazon ECS 部署的「掛鉤」部分

{ "version": 0.0, "Resources": [ { "TargetService": { "Type": "AWS::ECS::Service", "Properties": { "TaskDefinition": "", "LoadBalancerInfo": { "ContainerName": "", "ContainerPort": }, "PlatformVersion": "", "NetworkConfiguration": { "AwsvpcConfiguration": { "Subnets": [ "", "" ], "SecurityGroups": [ "", "" ], "AssignPublicIp": "" } } } } } ], "Hooks": [ { "BeforeInstall": "" }, { "AfterInstall": "" }, { "AfterAllowTestTraffic": "" }, { "BeforeAllowTraffic": "" }, { "AfterAllowTraffic": "" } ] }

為 AWS Lambda 部署新增 AppSpec 檔案

若要部署至 AWS Lambda 運算平台:

  • 此 AppSpec 檔案包含要部署和用於部署驗證之 Lambda 函數的相關指示。

  • 修訂與 AppSpec 檔案相同。

  • 一個 AppSpec 文件可以使用 JSON 或 YAML 來寫入。

  • 建立部署時, AppSpec 檔案可以儲存為文字檔或直接輸入主控台 AppSpec 編輯器中。如需詳細資訊,請參閱 建立 AWS Lambda 運算平台部署 (主控台)

若要建立 AppSpec 檔案:

  1. 將 JSON 或 YAML 範本複製到文字編輯器或主控台的 AppSpec 編輯器中。

  2. 可視需要修改範本。

  3. 使用 JSON 或 YAML 驗證程式來驗證您的 AppSpec 檔案。如果使用 AppSpec編輯器,則在選擇「建立部署」時驗證檔案。

  4. 如果您是使用文字編輯器,請儲存該檔案。如果您使 AWS CLI 用建立部署,請參考檔案 (如果 AppSpec 檔案位於您的硬碟或 Amazon S3 儲存貯體中)。如果您使用主控台,則必須將 AppSpec 檔案推送到 Amazon S3。

具有指示的 AWS Lambda 部署的 YAML AppSpec 檔案範本

如需在關聯區段中使用生命週期事件的資訊,請參閱 AppSpec AWS Lambda 部署的「掛鉤」部分

# This is an appspec.yml template file for use with an AWS Lambda deployment in CodeDeploy. # The lines in this template starting with the hashtag symbol are # instructional comments and can be safely left in the file or # ignored. # For help completing this file, see the "AppSpec File Reference" in the # "CodeDeploy User Guide" at # https://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html version: 0.0 # In the Resources section specify the name, alias, # target version, and (optional) the current version of your AWS Lambda function. Resources: - MyFunction: # Replace "MyFunction" with the name of your Lambda function Type: AWS::Lambda::Function Properties: Name: "" # Specify the name of your Lambda function Alias: "" # Specify the alias for your Lambda function CurrentVersion: "" # Specify the current version of your Lambda function TargetVersion: "" # Specify the version of your Lambda function to deploy # (Optional) In the Hooks section, specify a validation Lambda function to run during # a lifecycle event. Replace "LifeCycleEvent" with BeforeAllowTraffic # or AfterAllowTraffic. Hooks: - LifeCycleEvent: "" # Specify a Lambda validation function between double-quotes.

AWS Lambda 部署範本的 JSON AppSpec 檔案

在下列範本中,將 "MyFunction" 取代為 AWS Lambda 函數的名稱。在選用的 Hook 區段中,將生命週期事件取代為 BeforeAllowTraffic 或 AfterAllowTraffic。

如需在關聯區段中使用生命週期事件的資訊,請參閱 AppSpec AWS Lambda 部署的「掛鉤」部分

{ "version": 0.0, "Resources": [{ "MyFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Name": "", "Alias": "", "CurrentVersion": "", "TargetVersion": "" } } }], "Hooks": [{ "LifeCycleEvent": "" } ] }

新增EC2/ AppSpec 內部部署的檔案

如果沒有 AppSpec 檔案, CodeDeploy 則無法將應用程式修訂版中的來源檔案對應至其目的地,或執行部署的指令碼至 EC2 /內部部署計算平台。

每個修訂只能包含一個 AppSpec 檔案。

若要將 AppSpec 檔案加入至修訂:

  1. 將範本複製到文字編輯器。

  2. 可視需要修改範本。

  3. 使用 YAML 驗證程式來檢查檔案的 AppSpec 有效性。

  4. 在修訂的根目錄中,將檔案儲存為 appspec.yml

  5. 執行下列其中一個指令,以確認您已將 AppSpec 檔案放置在根目錄中:

    • 若為 Linux、macOS 或 Unix:

      find /path/to/root/directory -name appspec.yml

      如果在那裡找不到 AppSpec 文件,則不會有輸出。

    • 針對 Windows:

      dir path\to\root\directory\appspec.yml

      如果檔案未儲存在該處,則會顯示「找不到 AppSpec 檔案」錯誤。

  6. 將修訂推送到 Amazon S3 或 GitHub.

    如需說明,請參閱將修訂推送 CodeDeploy 至 Amazon S3 (僅適用於 EC2 /內部部署)

AppSpec EC2/內部部署的檔案範本與指示

注意

對 Windows 伺服器執行個體的部署不支援該runas元素。如果您要部署到 Windows 伺服器執行個體,請勿將其包含在您的 AppSpec 檔案中。

# This is an appspec.yml template file for use with an EC2/On-Premises deployment in CodeDeploy. # The lines in this template starting with the hashtag symbol are # instructional comments and can be safely left in the file or # ignored. # For help completing this file, see the "AppSpec File Reference" in the # "CodeDeploy User Guide" at # https://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html version: 0.0 # Specify "os: linux" if this revision targets Amazon Linux, # Red Hat Enterprise Linux (RHEL), or Ubuntu Server # instances. # Specify "os: windows" if this revision targets Windows Server instances. # (You cannot specify both "os: linux" and "os: windows".) os: linux # os: windows # During the Install deployment lifecycle event (which occurs between the # BeforeInstall and AfterInstall events), copy the specified files # in "source" starting from the root of the revision's file bundle # to "destination" on the Amazon EC2 instance. # Specify multiple "source" and "destination" pairs if you want to copy # from multiple sources or to multiple destinations. # If you are not copying any files to the Amazon EC2 instance, then remove the # "files" section altogether. A blank or incomplete "files" section # may cause associated deployments to fail. files: - source: destination: - source: destination: # For deployments to Amazon Linux, Ubuntu Server, or RHEL instances, # you can specify a "permissions" # section here that describes special permissions to apply to the files # in the "files" section as they are being copied over to # the Amazon EC2 instance. # For more information, see the documentation. # If you are deploying to Windows Server instances, # then remove the # "permissions" section altogether. A blank or incomplete "permissions" # section may cause associated deployments to fail. permissions: - object: pattern: except: owner: group: mode: acls: - context: user: type: range: type: - # If you are not running any commands on the Amazon EC2 instance, then remove # the "hooks" section altogether. A blank or incomplete "hooks" section # may cause associated deployments to fail. hooks: # For each deployment lifecycle event, specify multiple "location" entries # if you want to run multiple scripts during that event. # You can specify "timeout" as the number of seconds to wait until failing the deployment # if the specified scripts do not run within the specified time limit for the # specified event. For example, 900 seconds is 15 minutes. If not specified, # the default is 1800 seconds (30 minutes). # Note that the maximum amount of time that all scripts must finish executing # for each individual deployment lifecycle event is 3600 seconds (1 hour). # Otherwise, the deployment will stop and CodeDeploy will consider the deployment # to have failed to the Amazon EC2 instance. Make sure that the total number of seconds # that are specified in "timeout" for all scripts in each individual deployment # lifecycle event does not exceed a combined 3600 seconds (1 hour). # For deployments to Amazon Linux, Ubuntu Server, or RHEL instances, # you can specify "runas" in an event to # run as the specified user. For more information, see the documentation. # If you are deploying to Windows Server instances, # remove "runas" altogether. # If you do not want to run any commands during a particular deployment # lifecycle event, remove that event declaration altogether. Blank or # incomplete event declarations may cause associated deployments to fail. # During the ApplicationStop deployment lifecycle event, run the commands # in the script specified in "location" starting from the root of the # revision's file bundle. ApplicationStop: - location: timeout: runas: - location: timeout: runas: # During the BeforeInstall deployment lifecycle event, run the commands # in the script specified in "location". BeforeInstall: - location: timeout: runas: - location: timeout: runas: # During the AfterInstall deployment lifecycle event, run the commands # in the script specified in "location". AfterInstall: - location: timeout: runas: - location: timeout: runas: # During the ApplicationStart deployment lifecycle event, run the commands # in the script specified in "location". ApplicationStart: - location: timeout: runas: - location: timeout: runas: # During the ValidateService deployment lifecycle event, run the commands # in the script specified in "location". ValidateService: - location: timeout: runas: - location: timeout: runas: