このハンズオンについて
- このハンズオンでは、Directory Serviceで作成したディレクトリのユーザでManagement Consoleにログインするまでの作業を実施します。
- 今回のハンズオンでは、AD-Connectorは対象外とします。
- 一部の操作はManagement Consoleで実施します。
- Macをお使いの方は、Windows Serverでリモートデスクトップ接続が出来るアプリをご用意ください。
- Windows Serverをドメインに参加させるため、SSMを使用します。SSMが利用可能なリージョンを利用してください。この手順ではオレゴンを利用します。
前提条件
-
この手順は、全部で5つの投稿から構成されており、本投稿は以下の投稿の内容を実施していることが前提となります。途中から実施した場合、変数が空欄のためにコマンドの実行に失敗する場合があります。
バージョン確認
このハンズオンは以下のバージョンで動作確認を行いました。
aws --version
aws-cli/1.9.5 Python/2.7.10 Linux/4.1.10-17.31.amzn1.x86_64 botocore/1.3.5
必要な権限
作業にあたっては、以下の権限を有したIAMユーザもしくはIAMロールを利用してください。
- EC2に対するフルコントロール権限
- Directory Serviceに関するフルコントロール権限
- IAMに関するフルコントロール権限
- SSMに関するフルコントロール権限
0. 準備
リージョンを指定
export AWS_DEFAULT_REGION='us-west-2'
資格情報を確認
aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************PZ4A iam-role
secret_key ****************AZ55 iam-role
region us-west-2 env AWS_DEFAULT_REGION
変数の確認
cat << ETX
DS_ID: ${DS_ID}
ROLE_NAME: ${ROLE_NAME}
INSTANCE_ID: ${INSTANCE_ID}
DS_VPC: "${VPC_ID}"
ETX
DS_ID: d-**********
ROLE_NAME: ds-role
INSTANCE_ID: i-********
DS_VPC: "vpc-********"
1. ディレクトリの削除
ロールの割り当てを削除
Management Consoleから実施
IAM Roleを選択し、「Remove Role」をクリック
「Remove」をクリック
Management Consoleへのアクセスを無効化
Management Consoleから実施
「Apps & Services」 → 「AWS Management Console」の「Manage Access」のリンクをクリック
「Disable Access」をクリック
以降は、通常通りCLIから実施
ディレクトリを削除
パラメータを確認
cat << ETX
DS_ID: ${DS_ID}
ETX
DS_ID: d-**********
削除
aws ds delete-directory --directory-id ${DS_ID}
{
"DirectoryId": "d-**********"
}
確認
aws ds describe-directories
StatusがDeletingであれば問題ありません。
{
"DirectoryDescriptions": [
{
"AccessUrl": "",
"DirectoryId": "d-**********",
"SsoEnabled": false,
"Name": "jawsug.local",
"DnsIpAddrs": [
"10.0.0.105",
"10.0.1.128"
],
"VpcSettings": {
"SubnetIds": [
"subnet-********",
"subnet-********"
],
"VpcId": "vpc-********",
"AvailabilityZones": [
"us-west-2a",
"us-west-2c"
]
},
"StageLastUpdatedDateTime": 1445096833.657,
"Alias": "",
"LaunchTime": 1445088952.272,
"StageReason": "User initiated directory deletion.",
"ShortName": "jawsug",
"Stage": "Deleting",
"Type": "SimpleAD",
"Size": "Small"
}
]
}
IAM Roleを削除
パラメータを確認
cat << ETX
ROLE_NAME: ${ROLE_NAME}
ETX
ROLE_NAME: ds-role
削除
aws iam delete-role --role-name ${ROLE_NAME}
(返値無し)
確認
aws iam get-role --role-name ${ROLE_NAME}
A client error (NoSuchEntity) occurred when calling the GetRole operation: The role with name ds-role cannot be found.
2. EC2インスタンスの削除
SSM Associationを削除
確認
aws ssm describe-association --name ${DOC_NAME} --instance-id ${INSTANCE_ID}
{
"AssociationDescription": {
"InstanceId": "i-********",
"Date": 1446441382.794,
"Name": "DomainJoin",
"Status": {
"Date": 1446441751.32,
"AdditionalInfo": "{\"lang\":\"en-US\",\"name\":\"EC2Config\",\"os\":\"Windows Server 2012 Standard\",\"osver\":\"6.2.9200\",\"ver\":\"3.10.442\"}",
"Message": "status:Passed, code:0, message:RuntimeStatusCounts=[Passed=1], RuntimeStatus=[aws:domainJoin={Passed}]",
"Name": "Success"
}
}
}
削除
aws ssm delete-association --name ${DOC_NAME} --instance-id ${INSTANCE_ID}
(返値無し)
確認
aws ssm describe-association --name ${DOC_NAME} --instance-id ${INSTANCE_ID}
A client error (AssociationDoesNotExist) occurred when calling the DescribeAssociation operation: None
EC2インスタンスを削除
パラメータを確認
cat << ETX
INSTANCE_ID: ${INSTANCE_ID}
ETX
INSTANCE_ID: i-********
削除
aws ec2 terminate-instances --instance-ids ${INSTANCE_ID}
{
"TerminatingInstances": [
{
"InstanceId": "i-********",
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
確認
aws ec2 describe-instances --instance-ids ${INSTANCE_ID}
Stateがshutting-downやterminatedであればOK
{
"Reservations": [
{
"OwnerId": "************",
"ReservationId": "r-********",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "",
"Platform": "windows",
"State": {
"Code": 48,
"Name": "terminated"
},
"EbsOptimized": false,
"LaunchTime": "2015-10-17T13:28:19.000Z",
"ProductCodes": [],
"StateTransitionReason": "User initiated (2015-10-17 16:00:38 GMT)",
"InstanceId": "i-********",
"ImageId": "ami-4623a846",
"PrivateDnsName": "",
"KeyName": "UserManagementServer",
"SecurityGroups": [],
"ClientToken": "",
"InstanceType": "t2.medium",
"NetworkInterfaces": [],
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-west-2a"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [],
"Architecture": "x86_64",
"StateReason": {
"Message": "Client.UserInitiatedShutdown: User initiated shutdown",
"Code": "Client.UserInitiatedShutdown"
},
"RootDeviceName": "/dev/sda1",
"VirtualizationType": "hvm",
"RootDeviceType": "ebs",
"AmiLaunchIndex": 0
}
]
}
]
}
3. SSM Documentの削除
確認
aws ssm list-documents
{
"DocumentIdentifiers": [
{
"Name": "DomainJoin",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-ConfigureCloudWatch",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-ConfigureWindowsUpdate",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-InstallApplication",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-InstallPowerShellModule",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-JoinDirectoryServiceDomain",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-RunPowerShellScript",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-UpdateEC2Config",
"PlatformTypes": [
"Windows"
]
}
]
}
削除
aws ssm delete-document --name ${DOC_NAME}
(返値無し)
確認
aws ssm list-documents
{
"DocumentIdentifiers": [
{
"Name": "AWS-ConfigureCloudWatch",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-ConfigureWindowsUpdate",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-InstallApplication",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-InstallPowerShellModule",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-JoinDirectoryServiceDomain",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-RunPowerShellScript",
"PlatformTypes": [
"Windows"
]
},
{
"Name": "AWS-UpdateEC2Config",
"PlatformTypes": [
"Windows"
]
}
]
}
4. インスタンスプロファイルおよびIAMロールの削除
確認(インスタンスプロファイル)
aws iam get-instance-profile --instance-profile-name ${SSM_ROLE_NAME}
{
"InstanceProfile": {
"InstanceProfileId": "A********************",
"Roles": [
{
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
},
"RoleId": "A********************",
"CreateDate": "2015-11-02T02:49:14Z",
"RoleName": "ssm-role",
"Path": "/",
"Arn": "arn:aws:iam::************:role/ssm-role"
}
],
"CreateDate": "2015-11-02T02:50:13Z",
"InstanceProfileName": "ssm-role",
"Path": "/",
"Arn": "arn:aws:iam::************:instance-profile/ssm-role"
}
}
インスタンスプロファイルからIAMロールを削除
aws iam remove-role-from-instance-profile --instance-profile-name ${SSM_ROLE_NAME} --role-name ${SSM_ROLE_NAME}
インスタンスプロファイルの削除
aws iam delete-instance-profile --instance-profile-name ${SSM_ROLE_NAME}
(返値無し)
確認(Instance Profile)
aws iam get-instance-profile --instance-profile-name ${SSM_ROLE_NAME}
A client error (NoSuchEntity) occurred when calling the GetInstanceProfile operation: Instance Profile ssm-role cannot be found.
確認(IAM Role)
aws iam list-attached-role-policies --role-name ${SSM_ROLE_NAME}
{
"AttachedPolicies": [
{
"PolicyName": "AmazonSSMFullAccess",
"PolicyArn": "arn:aws:iam::aws:policy/AmazonSSMFullAccess"
}
]
}
デタッチ
aws iam detach-role-policy --role-name ${SSM_ROLE_NAME} --policy-arn ${SSM_POLICY_ARN}
(返値無し)
削除
aws iam delete-role --role-name ${SSM_ROLE_NAME}
(返値無し)
確認
aws iam list-attached-role-policies --role-name ${SSM_ROLE_NAME}
A client error (NoSuchEntity) occurred when calling the ListAttachedRolePolicies operation: Role ssm-role does not exist.
5. Key Pairおよび秘密鍵ファイルの削除
確認
aws ec2 describe-key-pairs --key-names ${KEY_PAIR_NAME}
ls -al ~/.ssh | grep ${KEY_MATERIAL_FILE}
{
"KeyPairs": [
{
"KeyName": "UserManagementServer",
"KeyFingerprint": "**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:**"
}
]
}
-rw-rw-r-- 1 ec2-user ec2-user 1671 Nov 2 02:40 key.pem
削除
aws ec2 delete-key-pair --key-name ${KEY_PAIR_NAME}
rm ~/.ssh/${KEY_MATERIAL_FILE}
(返値無し)
確認
aws ec2 describe-key-pairs --key-names ${KEY_PAIR_NAME}
ls -al ~/.ssh | grep ${KEY_MATERIAL_FILE}
A client error (InvalidKeyPair.NotFound) occurred when calling the DescribeKeyPairs operation: The key pair 'UserManagementServer' does not exist
6. VPCおよびVPCに関連するリソースの削除
セキュリティグループの削除
確認
aws ec2 describe-security-groups --group-ids ${SG_ID}
{
"SecurityGroups": [
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"PrefixListIds": []
}
],
"Description": "RemoteDesktop",
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 3389,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 3389,
"IpProtocol": "tcp",
"UserIdGroupPairs": []
}
],
"GroupName": "WindowsServer",
"VpcId": "vpc-********",
"OwnerId": "************",
"GroupId": "sg-********"
}
]
}
削除
aws ec2 delete-security-group --group-id ${SG_ID}
(返値無し)
確認
aws ec2 describe-security-groups --group-ids ${SG_ID}
A client error (InvalidGroup.NotFound) occurred when calling the DescribeSecurityGroups operation: The security group 'sg-********' does not exist
Subnetの削除
確認
aws ec2 describe-subnets --subnet-ids ${SUBNET_A_ID} ${SUBNET_C_ID}
{
"Subnets": [
{
"VpcId": "vpc-********",
"CidrBlock": "10.0.0.0/24",
"MapPublicIpOnLaunch": false,
"DefaultForAz": false,
"State": "available",
"AvailabilityZone": "us-west-2a",
"SubnetId": "subnet-********",
"AvailableIpAddressCount": 251
},
{
"VpcId": "vpc-********",
"CidrBlock": "10.0.1.0/24",
"MapPublicIpOnLaunch": false,
"DefaultForAz": false,
"State": "available",
"AvailabilityZone": "us-west-2c",
"SubnetId": "subnet-********",
"AvailableIpAddressCount": 251
}
]
}
削除
aws ec2 delete-subnet --subnet-id ${SUBNET_A_ID}
aws ec2 delete-subnet --subnet-id ${SUBNET_C_ID}
(返値無し)
確認
aws ec2 describe-subnets --subnet-ids ${SUBNET_A_ID}
aws ec2 describe-subnets --subnet-ids ${SUBNET_C_ID}
A client error (InvalidSubnetID.NotFound) occurred when calling the DescribeSubnets operation: The subnet ID 'subnet-********' does not exist
A client error (InvalidSubnetID.NotFound) occurred when calling the DescribeSubnets operation: The subnet ID 'subnet-********' does not exist
Internet Gatewayのデタッチ、削除
確認
aws ec2 describe-internet-gateways --internet-gateway-ids ${IGW_ID}
{
"InternetGateways": [
{
"Tags": [
{
"Value": "jawsug-cli",
"Key": "Name"
}
],
"InternetGatewayId": "igw-********",
"Attachments": [
{
"State": "available",
"VpcId": "vpc-********"
}
]
}
]
}
デタッチ
aws ec2 detach-internet-gateway --internet-gateway-id ${IGW_ID} --vpc-id ${VPC_ID}
(返値無し)
確認
aws ec2 describe-internet-gateways --internet-gateway-ids ${IGW_ID}
{
"InternetGateways": [
{
"Tags": [
{
"Value": "jawsug-cli",
"Key": "Name"
}
],
"InternetGatewayId": "igw-********",
"Attachments": []
}
]
}
削除
aws ec2 delete-internet-gateway --internet-gateway-id ${IGW_ID}
(返値無し)
確認
aws ec2 describe-internet-gateways --internet-gateway-ids ${IGW_ID}
A client error (InvalidInternetGatewayID.NotFound) occurred when calling the DescribeInternetGateways operation: The internetGateway ID 'igw-********' does not exist
VPCの削除
確認
aws ec2 describe-vpcs --vpc-ids ${VPC_ID}
{
"Vpcs": [
{
"VpcId": "vpc-********",
"InstanceTenancy": "default",
"Tags": [
{
"Value": "jawsug-cli",
"Key": "Name"
}
],
"State": "available",
"DhcpOptionsId": "dopt-********",
"CidrBlock": "10.0.0.0/16",
"IsDefault": false
}
]
}
削除
aws ec2 delete-vpc --vpc-id ${VPC_ID}
(返値無し)
確認
aws ec2 describe-vpcs --vpc-ids ${VPC_ID}
A client error (InvalidVpcID.NotFound) occurred when calling the DescribeVpcs operation: The vpc ID 'vpc-********' does not exist
DHCP Optionsの削除
確認
aws ec2 describe-dhcp-options --dhcp-options-ids ${DHCP_ID}
{
"DhcpOptions": [
{
"DhcpConfigurations": [
{
"Values": [
{
"Value": "jawsug.local"
}
],
"Key": "domain-name"
},
{
"Values": [
{
"Value": "10.0.0.98"
},
{
"Value": "10.0.1.238"
}
],
"Key": "domain-name-servers"
}
],
"DhcpOptionsId": "dopt-********"
}
]
}
削除
aws ec2 delete-dhcp-options --dhcp-options-id ${DHCP_ID}
(返値無し)
確認
aws ec2 describe-dhcp-options --dhcp-options-ids ${DHCP_ID}
A client error (InvalidDhcpOptionID.NotFound) occurred when calling the DescribeDhcpOptions operation: The dhcpOption ID 'dopt-********' does not exist
以上です。お疲れ様でした。