AWS CLIを使用した Amazon EC2 セキュリティグループとインスタンスタイプの設定 - AWS Elastic Beanstalk

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

AWS CLIを使用した Amazon EC2 セキュリティグループとインスタンスタイプの設定

AWS コマンドラインインターフェイス (AWS CLI) を使用して、Elastic Beanstalk 環境で Amazon EC2 インスタンスを設定できます。

を使用した EC2 セキュリティグループの設定 AWS CLI

このトピックでは、単一インスタンス環境と負荷分散された (複数インスタンス) 環境の両方の異なる EC2 セキュリティグループ設定の例を提供します。これらの例のオプションの詳細については、「aws:autoscaling:launchconfiguration」を参照してください。

注意事項

環境の作成オペレーションは、デフォルトで EC2 セキュリティグループを提供します。また、デフォルトで Application Load Balancer を備えた環境を作成します。

更新環境オペレーションは、ブール値オプション DisableDefaultEC2SecurityGroup を使用して、環境のデフォルトの EC2 セキュリティグループを無効または有効にするために使用できます。例 5 は、以前に環境を変更したことがある場合に、環境をデフォルトのセキュリティ設定に戻す方法を示しています。

次の例は、デフォルトの EC2 セキュリティグループからオプトアウトし、代わりにカスタムセキュリティグループを提供する create-environment コマンドを示しています。DisableDefaultEC2SecurityGroup オプションは true に設定されているため、Elastic Beanstalk が通常 EC2 インスタンスに関連付けるデフォルトの EC2 セキュリティグループは作成されません。したがって、SecurityGroups オプションを他のセキュリティグループに提供する必要があります。

aws:elasticbeanstalk:environment EnvironmentType オプションは SingleInstance に設定されていることに注意してください。LoadBalanced はデフォルトの EnvironmentType であるため、単一のインスタンス環境を作成するには、このオプションを指定する必要があります。この環境にはロードバランサーが含まれていないため、ロードバランサーのセキュリティグループを指定する必要はありません。

例 1 — カスタム EC2 セキュリティグループを持つ新しい単一インスタンス環境 (インラインの名前空間オプション)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2023 v6.5.0 applrunning Node.js 22" \ --option-settings \ Namespace=aws:elasticbeanstalk:environment,OptionName=EnvironmentType,Value=SingleInstance \ Namespace=aws:autoscaling:launchconfiguration,OptionName=IamInstanceProfile,Value=aws-elasticbeanstalk-ec2-role \ Namespace=aws:autoscaling:launchconfiguration,OptionName=DisableDefaultEC2SecurityGroup,Value=true \ Namespace=aws:autoscaling:launchconfiguration,OptionName=SecurityGroups,Value=sg-abcdef01, sg-abcdef02 \ Namespace=aws:autoscaling:launchconfiguration,OptionName=EC2KeyName,Value=my-keypair

別の方法として、options.jsonファイルを使用して、インラインで名前空間オプションを含めるのではなく、名前空間オプションを指定します。

例 2 — カスタム EC2 セキュリティグループを持つ新しい単一インスタンス環境 (options.json ファイルの名前空間オプション)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2023 v6.5.0 running Node.js 22" \ --option-settings file://options.json
### example options.json ### [ { "Namespace" : "aws:elasticbeanstalk:environment", "OptionName" : "EnvironmentType", "Value" : "SingleInstance" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "true" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "SecurityGroups", "Value": "sg-abcdef01, sg-abcdef02" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "EC2KeyName", "Value": "my-keypair" } ]

次の例では、負荷分散された環境を作成します。application に設定された aws:elasticbeanstalk:environment 名前空間オプション LoadBalancerType を指定します。DisableDefaultEC2SecurityGroup オプションを使用してデフォルトの EC2 セキュリティグループを無効にするため、前の例のように、aws:autoscaling:launchconfiguration SecurityGroups オプションを使用して EC2 インスタンスに独自のカスタムセキュリティグループを再度指定する必要があります。この環境にはトラフィックをルーティングするロードバランサーがあるため、ロードバランサーのセキュリティグループも提供する必要があります。

Classic Load Balancer を使用し、それ以外は同じ設定の環境を作成するには、aws:elasticbeanstalk:environment 名前空間オプション LoadBalancerType の設定を classic に更新します。

異なるロードバランサーのタイプには、セキュリティグループを指定するオプションを保持する名前空間がそれぞれ異なって存在します。

例 3 — カスタム EC2 セキュリティグループを持つ新しいマルチインスタンス環境 (options.json ファイルの名前空間オプション)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2023 v6.5.0 running Node.js 22" \ --option-settings file://options.json
### example options.json ### [ { "Namespace" : "aws:elasticbeanstalk:environment", "OptionName" : "EnvironmentType", "Value" : "LoadBalanced" }, { "Namespace" : "aws:elasticbeanstalk:environment", "OptionName" : "LoadBalancerType", "Value" : "application" }, { "Namespace" : "aws:elbv2:loadbalancer", "OptionName" : "SecurityGroups", "Value" : "sg-abcdefghikl012345" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "true" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "SecurityGroups", "Value": "sg-abcdef01, sg-abcdef02" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "EC2KeyName", "Value": "my-keypair" } ]

update-environment コマンドを使用して、既存の環境のデフォルトの EC2 セキュリティグループを無効にすることができます。次のコマンド例では、デフォルトの EC2 セキュリティグループを無効にし、環境の EC2 インスタンスでカスタム EC2 セキュリティグループを割り当てます。

環境がロードバランシングされているかどうかとロードバランサーのタイプに応じて、例 4 (a)、4 (b)、または 4 (c) の options.jason ファイルの例を使用します。設定ファイル 4 (a) は、単一インスタンス環境のセキュリティグループを指定します。ロードバランサーを必要としないため、EC2 インスタンスのセキュリティグループのみが提供されます。設定ファイル 4 (b) と 4 (c) は、Application Load Balancer と Classic Load Balancer のセキュリティグループを指定します。このような場合は、ロードバランサーのセキュリティグループも指定する必要があります。

例 4 — 既存の環境を更新してデフォルトの EC2 セキュリティグループを無効にする (options.json ファイルの名前空間オプション)
aws elasticbeanstalk update-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2023 v6.5.0 running Node.js 22" \ --option-settings file://options.json
例 4 (a) — 単一インスタンス環境の設定ファイル (ロードバランサーなし)
### example options.json ### [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "true" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "SecurityGroups", "Value": "sg-abcdef01, sg-abcdef02" } ]

Application Load Balancer を使用している環境を更新するには、aws:elbv2:loadbalancer 名前空間を使用してロードバランサーのセキュリティグループを指定します。

例 4 (b) — Application Load Balancer を使用する環境の設定ファイル
### example options.json ### [ { "Namespace" : "aws:elbv2:loadbalancer", "OptionName" : "SecurityGroups", "Value" : "sg-abcdefghikl012345" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "true" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "SecurityGroups", "Value": "sg-abcdef01, sg-abcdef02" } ]

Classic Load Balancer を使用していする環境を更新するには、aws:elb:loadbalancer 名前空間を使用してロードバランサーのセキュリティグループを指定します。

例 4 (c) — Classic Load Balancer を使用する環境の設定ファイル
### example options.json ### [ { "Namespace" : "aws:elb:loadbalancer", "OptionName" : "SecurityGroups", "Value" : "sg-abcdefghikl012345" }, { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "true" }, { "Namespace": "aws:autoscaling:launchconfiguration",n "OptionName": "SecurityGroups", "Value": "sg-abcdef01, sg-abcdef02" } ]

Elastic Beanstalk が割り当てるデフォルトのセキュリティグループを使用して環境をデフォルトの動作と設定に戻すには、update-environment コマンドを使用して DisableDefaultEC2SecurityGroupfalse に設定します。マルチインスタンス環境の場合、Elastic Beanstalk は環境のロードバランサーのセキュリティグループとネットワークトラフィックルールも処理します。

次の例は、単一インスタンス環境またはマルチインスタンス (負荷分散された) 環境の両方に適用されます。

例 5 — デフォルトのセキュリティグループを使用して環境を更新し直す (options.json ファイルの名前空間オプション)
aws elasticbeanstalk update-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2023 v6.5.0 running Node.js 22" \ --option-settings file://options.json
### example options.json ### [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "DisableDefaultEC2SecurityGroup", "Value": "false" } ]

を使用してインスタンスタイプで EC2 を設定する AWS CLI

このトピックでは、環境内の EC2 インスタンスのインスタンスタイプを設定するための例を提供します。

最初の 2 つの例では、新しい環境を作成します。このコマンドは、arm64 プロセッサアーキテクチャに基づく Amazon EC2 インスタンスタイプ t4g.small を指定します。Elastic Beanstalk は、リージョン、プラットフォームのバージョン、インスタンスタイプに基づいて EC2 インスタンスのイメージ ID(AMI)をデフォルト設定します。インスタンスタイプは、プロセッサアーキテクチャに対応します。-solution-stack-nameパラメータはプラットフォームバージョンに適用されます。

例 1 — 新しい arm64 ベースの環境を作成する (名前空間オプションをインライン)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings \ Namespace=aws:autoscaling:launchconfiguration,OptionName=IamInstanceProfile,Value=aws-elasticbeanstalk-ec2-role \ Namespace=aws:ec2:instances,OptionName=InstanceTypes,Value=t4g.small

別の方法として、options.jsonファイルを使用して、インラインで名前空間オプションを含めるのではなく、名前空間オプションを指定します。

例 2 — 新しい arm64 ベースの環境 (名前空間オプション) を作成します。options.jsonファイル)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings file://options.json
### example options.json ### [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" }, { "Namespace": "aws:ec2:instances", "OptionName": "InstanceTypes", "Value": "t4g.small" } ]

次の 2 つの例では、既存の環境の設定をupdate-environmentコマンド。この例では、arm64 プロセッサアーキテクチャに基づく別のインスタンスタイプを追加します。既存の環境では、追加されるすべてのインスタンスタイプが同じプロセッサアーキテクチャを持つ必要があります。既存のインスタンスタイプを別のアーキテクチャのインスタンスタイプに置き換えることもできます。ただし、コマンド内のすべてのインスタンスタイプが同じタイプのアーキテクチャであることを確認します。

例 3 — 既存の arm64 ベースの環境を更新する (名前空間オプションをインライン)
aws elasticbeanstalk update-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings \ Namespace=aws:autoscaling:launchconfiguration,OptionName=IamInstanceProfile,Value=aws-elasticbeanstalk-ec2-role \ Namespace=aws:ec2:instances,OptionName=InstanceTypes,Value=t4g.small,t4g.micro

別の方法として、options.jsonファイルを使用して、インラインで名前空間オプションを含めるのではなく、名前空間オプションを指定します。

例 4 — 既存の arm64 ベースの環境を更新する (名前空間オプション)options.jsonファイル)
aws elasticbeanstalk update-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings file://options.json
### example options.json ### [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" }, { "Namespace": "aws:ec2:instances", "OptionName": "InstanceTypes", "Value": "t4g.small, t4g.micro" } ]

次の 2 つの例で詳しく説明します。環境を作成するコマンド。これらの例は、の値を提供しません。InstanceTypes。メトリックInstanceTypes値は指定されていません。Elastic Beanstalk はデフォルトで x86 ベースのプロセッサアーキテクチャです。環境の EC2 インスタンスのイメージ ID(AMI)は、リージョン、プラットフォームのバージョン、およびデフォルトのインスタンスタイプに従ってデフォルト設定されます。インスタンスタイプは、プロセッサアーキテクチャに対応します。

例 5 — 新しい x86 ベースの環境を作成する (名前空間オプションをインライン)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings \ Namespace=aws:autoscaling:launchconfiguration,OptionName=IamInstanceProfile,Value=aws-elasticbeanstalk-ec2-role

別の方法として、options.jsonファイルを使用して、インラインで名前空間オプションを含めるのではなく、名前空間オプションを指定します。

例 6 — 新しい x86 ベースの環境 (名前空間オプション) を作成します。options.jsonファイル)
aws elasticbeanstalk create-environment \ --region us-east-1 \ --application-name my-app \ --environment-name my-env \ --solution-stack-name "64bit Amazon Linux 2 v3.4.7 running Docker" \ --option-settings file://options.json
### example options.json ### [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" } ]