

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# CodeDeploy 用のアプリケーション仕様ファイルをリビジョンに追加
<a name="application-revisions-appspec-file"></a>

このトピックでは、AppSpec ファイルをデプロイに追加する方法について説明します。また、 AWS Lambda および EC2/オンプレミスデプロイ用の AppSpec ファイルを作成するためのテンプレートも含まれています。

**Topics**
+ [Amazon ECS デプロイ用の AppSpec ファイルを追加する](#add-appspec-file-ecs)
+ [AWS Lambda デプロイ用の AppSpec ファイルを追加する](#add-appspec-file-lambda)
+ [EC2/オンプレミスデプロイに AppSpec ファイルを追加する](#add-appspec-file-server)

## Amazon ECS デプロイ用の AppSpec ファイルを追加する
<a name="add-appspec-file-ecs"></a>

Amazon ECS コンピューティングプラットフォームへのデプロイの場合:
+ AppSpec ファイルは、デプロイに使用される Amazon ECS タスク定義、トラフィックをルーティングするコンテナ名とポートマッピング、およびオプションとしてデプロイライフサイクルイベントの後で実行される Lambda 関数を指定します。
+ リビジョンは、AppSpec ファイルと同じです。
+ AppSpec ファイルは、JSON または YAML を使用して書き込むことができます。
+ AppSpec ファイルは、テキストファイルとして保存するか、デプロイ作成時にコンソールに直接入力することができます。詳細については、「[Amazon ECS コンピューティングプラットフォームのデプロイの作成 (コンソール)](deployments-create-console-ecs.md)」を参照してください。

**AppSpec ファイルを作成するには**

1. JSON または YAML テンプレートをテキストエディタ、またはコンソールの AppSpec エディタにコピーします。

1. 必要に応じてテンプレートを変更します。

1. JSON または YAML バリデータを使用して、AppSpec ファイルを検証します。AppSpec エディタを使用している場合は、[**デプロイの作成**] を選択すると、このファイルが検証されます。

1. テキストエディタを使用している場合は、ファイルを保存します。を使用してデプロイ AWS CLI を作成する場合は、ハードドライブまたは Amazon S3 バケットにある AppSpec ファイルを参照してください。コンソールを使用している場合は、AppSpec ファイルを Amazon S3 にプッシュする必要があります。

### Amazon ECS デプロイ用の手順付き YAML AppSpec ファイルテンプレート
<a name="app-spec-template-yaml-ecs"></a>

以下に示しているのは、利用できるすべてのオプションを含む Amazon ECS デプロイ用の AppSpec ファイルの YAML テンプレートです。`hooks` セクションで使用するライフサイクルイベントについては、「[Amazon ECS のデプロイ向けの AppSpec の「hooks」セクション](reference-appspec-file-structure-hooks.md#appspec-hooks-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 ファイル
<a name="app-spec-template-json-ecs"></a>

以下に示しているのは、利用できるすべてのオプションを含む Amazon ECS デプロイ用の AppSpec ファイルの JSON テンプレートです。テンプレートの使用方法については、前のセクションの YAML バージョンのコメントを参照してください。`hooks` セクションで使用するライフサイクルイベントについては、「[Amazon ECS のデプロイ向けの AppSpec の「hooks」セクション](reference-appspec-file-structure-hooks.md#appspec-hooks-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 ファイルを追加する
<a name="add-appspec-file-lambda"></a>

 AWS Lambda コンピューティングプラットフォームへのデプロイの場合:
+ AppSpec ファイルには、デプロイされる Lambda 関数に関する手順が含まれており、デプロイ検証で使用されます。
+ リビジョンは、AppSpec ファイルと同じです。
+ AppSpec ファイルは、JSON または YAML を使用して書き込むことができます。
+ AppSpec ファイルは、テキストファイルとして保存するか、デプロイ作成時にコンソールの AppSpec エディタに直接入力することができます。詳細については、「[AWS Lambda コンピューティングプラットフォームのデプロイを作成する (コンソール)](deployments-create-console-lambda.md)」を参照してください。

AppSpec ファイルを作成するには

1. JSON または YAML テンプレートをテキストエディタ、またはコンソールの AppSpec エディタにコピーします。

1. 必要に応じてテンプレートを変更します。

1. JSON または YAML バリデータを使用して、AppSpec ファイルを検証します。AppSpec エディタを使用している場合は、[**デプロイの作成**] を選択すると、このファイルが検証されます。

1. テキストエディタを使用している場合は、ファイルを保存します。を使用してデプロイ AWS CLI を作成する場合は、ハードドライブまたは Amazon S3 バケットにある AppSpec ファイルを参照してください。コンソールを使用している場合は、AppSpec ファイルを Amazon S3 にプッシュする必要があります。



### AWS Lambda デプロイ用の YAML AppSpec ファイルテンプレートと手順
<a name="app-spec-template-yaml-lambda"></a>

hooks セクションで使用するライフサイクルイベントについては、「[AWS Lambda デプロイの AppSpec 'hooks' セクション](reference-appspec-file-structure-hooks.md#appspec-hooks-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 ファイル
<a name="app-spec-template-json-lambda"></a>

以下のテンプレートで、"MyFunction" を AWS Lambda 関数名に置き換えます。オプションの Hooks セクションで、ライフサイクルイベントを BeforeAllowTraffic または AfterAllowTraffic に置き換えます。

Hooks セクションで使用するライフサイクルイベントについては、「[AWS Lambda デプロイの AppSpec 'hooks' セクション](reference-appspec-file-structure-hooks.md#appspec-hooks-lambda)」を参照してください。

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

## EC2/オンプレミスデプロイに AppSpec ファイルを追加する
<a name="add-appspec-file-server"></a>

AppSpec ファイルがないと、CodeDeploy は、アプリケーションリビジョンのソースファイルを送信先にマッピングしたり、EC2/オンプレミスコンピューティングプラットフォームにデプロイするためのスクリプトを実行したりできません。

各リビジョンには、AppSpec ファイルを 1 つだけ含める必要があります。

AppSpec ファイルをリビジョンに追加するには:

1. テンプレートにテキストエディターをコピーします。

1. 必要に応じてテンプレートを変更します。

1. YAML バリデータを使用して、AppSpec ファイルの有効性をチェックします。

1. リビジョンのルートディレクトリに `appspec.yml` としてファイルを保存します。

1. AppSpec ファイルをルートディレクトリに配置したことを確認するには、次のいずれかのコマンドを実行します。
   + Linux、macOS、Unix の場合:

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

     AppSpec ファイルがその場所に見つからない場合は、何も出力されません。
   + Windows の場合:

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

     そこに AppSpec ファイルが保存されていない場合は、[**File Not Found**] エラーが表示されます。

1. リビジョンを Amazon S3 または GitHub にプッシュします。

   手順については、「[Amazon S3 に CodeDeploy のリビジョンをプッシュする (EC2/オンプレミスのデプロイのみ)](application-revisions-push.md)」を参照してください。

### EC2/オンプレミスデプロイ用の手順付き AppSpec ファイルテンプレート
<a name="app-spec-template-server"></a>

**注記**  
 Windows Server インスタンスへのデプロイでは、`runas` 要素をサポートしていません。Windows Server インスタンスにデプロイする場合は、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:
```