diff --git a/kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/ScenarioPartiQ.kt b/kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/ScenarioPartiQ.kt index 2df513cb58e..ad6b9bbf4e9 100644 --- a/kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/ScenarioPartiQ.kt +++ b/kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/ScenarioPartiQ.kt @@ -187,7 +187,6 @@ suspend fun updateTableItemPartiQL(ddb: DynamoDbClient) { // Query the table where the year is 2013. suspend fun queryTablePartiQL(ddb: DynamoDbClient) { val sqlStatement = "SELECT * FROM MoviesPartiQ where year = ?" - val parameters: MutableList = java.util.ArrayList() parameters.add(AttributeValue.N("2013")) val response = executeStatementPartiQL(ddb, sqlStatement, parameters) diff --git a/kotlin/services/dynamodb/src/test/kotlin/DynamoDB.kt b/kotlin/services/dynamodb/src/test/kotlin/DynamoDB.kt index 28ccbb33f2b..c1f4ed8b4ca 100644 --- a/kotlin/services/dynamodb/src/test/kotlin/DynamoDB.kt +++ b/kotlin/services/dynamodb/src/test/kotlin/DynamoDB.kt @@ -128,7 +128,7 @@ class DynamoDB { awards, awardVal, songTitle, - songTitleVal, + songTitleVal ) println("Test 3 passed") } diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/AllocateAddress.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/AllocateAddress.kt index 97622fc63b9..e172ab46d05 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/AllocateAddress.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/AllocateAddress.kt @@ -1,60 +1,62 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.allocate_address.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.AllocateAddressRequest -import aws.sdk.kotlin.services.ec2.model.AssociateAddressRequest -import aws.sdk.kotlin.services.ec2.model.DomainType -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.allocate_address.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - instanceId - An instance id value that you can obtain from the AWS Management Console. - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val instanceId = args[0] - val idValue = getAllocateAddress(instanceId) - println("The id value for the allocated elastic IP address is $idValue") -} - -// snippet-start:[ec2.kotlin.allocate_address.main] -suspend fun getAllocateAddress(instanceIdVal: String?): String? { - val allocateRequest = AllocateAddressRequest { - domain = DomainType.Vpc - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val allocateResponse = ec2.allocateAddress(allocateRequest) - val allocationIdVal = allocateResponse.allocationId - - val request = AssociateAddressRequest { - instanceId = instanceIdVal - allocationId = allocationIdVal - } - - val associateResponse = ec2.associateAddress(request) - return associateResponse.associationId - } -} -// snippet-end:[ec2.kotlin.allocate_address.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.allocate_address.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.AllocateAddressRequest +import aws.sdk.kotlin.services.ec2.model.AssociateAddressRequest +import aws.sdk.kotlin.services.ec2.model.DomainType +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.allocate_address.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + instanceId - An instance id value that you can obtain from the AWS Management Console. + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val instanceId = args[0] + val idValue = getAllocateAddress(instanceId) + println("The id value for the allocated elastic IP address is $idValue") +} + +// snippet-start:[ec2.kotlin.allocate_address.main] +suspend fun getAllocateAddress(instanceIdVal: String?): String? { + val allocateRequest = + AllocateAddressRequest { + domain = DomainType.Vpc + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val allocateResponse = ec2.allocateAddress(allocateRequest) + val allocationIdVal = allocateResponse.allocationId + + val request = + AssociateAddressRequest { + instanceId = instanceIdVal + allocationId = allocationIdVal + } + + val associateResponse = ec2.associateAddress(request) + return associateResponse.associationId + } +} +// snippet-end:[ec2.kotlin.allocate_address.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateInstance.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateInstance.kt index 25ace94e2a3..4e61e5729b5 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateInstance.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateInstance.kt @@ -1,69 +1,75 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.create_instance.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.CreateTagsRequest -import aws.sdk.kotlin.services.ec2.model.InstanceType -import aws.sdk.kotlin.services.ec2.model.RunInstancesRequest -import aws.sdk.kotlin.services.ec2.model.Tag -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.create_instance.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - name - An instance name that you can obtain from the AWS Management Console (for example, ami-xxxxxx5c8b987b1a0). - amiId - An Amazon Machine Image (AMI) value that you can obtain from the AWS Management Console (for example, i-xxxxxx2734106d0ab). - """ - - if (args.size != 2) { - println(usage) - exitProcess(0) - } - - val name = args[0] - val amiId = args[1] - createEC2Instance(name, amiId) -} - -// snippet-start:[ec2.kotlin.create_instance.main] -suspend fun createEC2Instance(name: String, amiId: String): String? { - val request = RunInstancesRequest { - imageId = amiId - instanceType = InstanceType.T1Micro - maxCount = 1 - minCount = 1 - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.runInstances(request) - val instanceId = response.instances?.get(0)?.instanceId - val tag = Tag { - key = "Name" - value = name - } - - val requestTags = CreateTagsRequest { - resources = listOf(instanceId.toString()) - tags = listOf(tag) - } - ec2.createTags(requestTags) - println("Successfully started EC2 Instance $instanceId based on AMI $amiId") - return instanceId - } -} -// snippet-end:[ec2.kotlin.create_instance.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.create_instance.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.CreateTagsRequest +import aws.sdk.kotlin.services.ec2.model.InstanceType +import aws.sdk.kotlin.services.ec2.model.RunInstancesRequest +import aws.sdk.kotlin.services.ec2.model.Tag +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.create_instance.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + name - An instance name that you can obtain from the AWS Management Console (for example, ami-xxxxxx5c8b987b1a0). + amiId - An Amazon Machine Image (AMI) value that you can obtain from the AWS Management Console (for example, i-xxxxxx2734106d0ab). + """ + + if (args.size != 2) { + println(usage) + exitProcess(0) + } + + val name = args[0] + val amiId = args[1] + createEC2Instance(name, amiId) +} + +// snippet-start:[ec2.kotlin.create_instance.main] +suspend fun createEC2Instance( + name: String, + amiId: String, +): String? { + val request = + RunInstancesRequest { + imageId = amiId + instanceType = InstanceType.T1Micro + maxCount = 1 + minCount = 1 + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.runInstances(request) + val instanceId = response.instances?.get(0)?.instanceId + val tag = + Tag { + key = "Name" + value = name + } + + val requestTags = + CreateTagsRequest { + resources = listOf(instanceId.toString()) + tags = listOf(tag) + } + ec2.createTags(requestTags) + println("Successfully started EC2 Instance $instanceId based on AMI $amiId") + return instanceId + } +} +// snippet-end:[ec2.kotlin.create_instance.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateKeyPair.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateKeyPair.kt index 43f7785d5f5..c1bc846aa5d 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateKeyPair.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateKeyPair.kt @@ -1,49 +1,50 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.create_key_pair.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.CreateKeyPairRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.create_key_pair.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - keyName - A key pair name (for example, TestKeyPair). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val keyName = args[0] - createEC2KeyPair(keyName) -} - -// snippet-start:[ec2.kotlin.create_key_pair.main] -suspend fun createEC2KeyPair(keyNameVal: String) { - val request = CreateKeyPairRequest { - keyName = keyNameVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.createKeyPair(request) - println("The key ID is ${response.keyPairId}") - } -} -// snippet-end:[ec2.kotlin.create_key_pair.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.create_key_pair.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.CreateKeyPairRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.create_key_pair.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + keyName - A key pair name (for example, TestKeyPair). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val keyName = args[0] + createEC2KeyPair(keyName) +} + +// snippet-start:[ec2.kotlin.create_key_pair.main] +suspend fun createEC2KeyPair(keyNameVal: String) { + val request = + CreateKeyPairRequest { + keyName = keyNameVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.createKeyPair(request) + println("The key ID is ${response.keyPairId}") + } +} +// snippet-end:[ec2.kotlin.create_key_pair.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateSecurityGroup.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateSecurityGroup.kt index 7b87ab62af7..5693f267a45 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateSecurityGroup.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateSecurityGroup.kt @@ -1,82 +1,91 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.create_security_group.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.AuthorizeSecurityGroupIngressRequest -import aws.sdk.kotlin.services.ec2.model.CreateSecurityGroupRequest -import aws.sdk.kotlin.services.ec2.model.IpPermission -import aws.sdk.kotlin.services.ec2.model.IpRange -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.create_security_group.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - Usage: - - - Where: - groupName - A group name (for example, TestKeyPair). - groupDesc - A group description (for example, TestKeyPair). - vpc-id - A VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f). - """ - - if (args.size != 3) { - println(usage) - exitProcess(0) - } - - val groupName = args[0] - val groupDesc = args[1] - val vpcId = args[2] - val id = createEC2SecurityGroup(groupName, groupDesc, vpcId) - println("Successfully created Security Group with ID $id") -} - -// snippet-start:[ec2.kotlin.create_security_group.main] -suspend fun createEC2SecurityGroup(groupNameVal: String?, groupDescVal: String?, vpcIdVal: String?): String? { - val request = CreateSecurityGroupRequest { - groupName = groupNameVal - description = groupDescVal - vpcId = vpcIdVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val resp = ec2.createSecurityGroup(request) - val ipRange = IpRange { - cidrIp = "0.0.0.0/0" - } - - val ipPerm = IpPermission { - ipProtocol = "tcp" - toPort = 80 - fromPort = 80 - ipRanges = listOf(ipRange) - } - - val ipPerm2 = IpPermission { - ipProtocol = "tcp" - toPort = 22 - fromPort = 22 - ipRanges = listOf(ipRange) - } - - val authRequest = AuthorizeSecurityGroupIngressRequest { - groupName = groupNameVal - ipPermissions = listOf(ipPerm, ipPerm2) - } - ec2.authorizeSecurityGroupIngress(authRequest) - println("Successfully added ingress policy to Security Group $groupNameVal") - return resp.groupId - } -} -// snippet-end:[ec2.kotlin.create_security_group.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.create_security_group.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.AuthorizeSecurityGroupIngressRequest +import aws.sdk.kotlin.services.ec2.model.CreateSecurityGroupRequest +import aws.sdk.kotlin.services.ec2.model.IpPermission +import aws.sdk.kotlin.services.ec2.model.IpRange +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.create_security_group.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + groupName - A group name (for example, TestKeyPair). + groupDesc - A group description (for example, TestKeyPair). + vpc-id - A VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f). + """ + + if (args.size != 3) { + println(usage) + exitProcess(0) + } + + val groupName = args[0] + val groupDesc = args[1] + val vpcId = args[2] + val id = createEC2SecurityGroup(groupName, groupDesc, vpcId) + println("Successfully created Security Group with ID $id") +} + +// snippet-start:[ec2.kotlin.create_security_group.main] +suspend fun createEC2SecurityGroup( + groupNameVal: String?, + groupDescVal: String?, + vpcIdVal: String?, +): String? { + val request = + CreateSecurityGroupRequest { + groupName = groupNameVal + description = groupDescVal + vpcId = vpcIdVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val resp = ec2.createSecurityGroup(request) + val ipRange = + IpRange { + cidrIp = "0.0.0.0/0" + } + + val ipPerm = + IpPermission { + ipProtocol = "tcp" + toPort = 80 + fromPort = 80 + ipRanges = listOf(ipRange) + } + + val ipPerm2 = + IpPermission { + ipProtocol = "tcp" + toPort = 22 + fromPort = 22 + ipRanges = listOf(ipRange) + } + + val authRequest = + AuthorizeSecurityGroupIngressRequest { + groupName = groupNameVal + ipPermissions = listOf(ipPerm, ipPerm2) + } + ec2.authorizeSecurityGroupIngress(authRequest) + println("Successfully added ingress policy to Security Group $groupNameVal") + return resp.groupId + } +} +// snippet-end:[ec2.kotlin.create_security_group.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteKeyPair.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteKeyPair.kt index 7842f45b158..743fd5d0f1d 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteKeyPair.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteKeyPair.kt @@ -1,49 +1,50 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.delete_key_pair.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DeleteKeyPairRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.delete_key_pair.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - keyName - A key pair name (for example, TestKeyPair). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val keyName = args[0] - deleteKeys(keyName) -} - -// snippet-start:[ec2.kotlin.delete_key_pair.main] -suspend fun deleteKeys(keyPair: String?) { - val request = DeleteKeyPairRequest { - keyName = keyPair - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.deleteKeyPair(request) - println("Successfully deleted key pair named $keyPair") - } -} -// snippet-end:[ec2.kotlin.delete_key_pair.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.delete_key_pair.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DeleteKeyPairRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.delete_key_pair.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + keyName - A key pair name (for example, TestKeyPair). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val keyName = args[0] + deleteKeys(keyName) +} + +// snippet-start:[ec2.kotlin.delete_key_pair.main] +suspend fun deleteKeys(keyPair: String?) { + val request = + DeleteKeyPairRequest { + keyName = keyPair + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.deleteKeyPair(request) + println("Successfully deleted key pair named $keyPair") + } +} +// snippet-end:[ec2.kotlin.delete_key_pair.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteSecurityGroup.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteSecurityGroup.kt index 16dc2938a3f..3632ae4ac21 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteSecurityGroup.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DeleteSecurityGroup.kt @@ -1,48 +1,49 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.delete_security_group.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DeleteSecurityGroupRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.delete_security_group.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - Usage: - - - Where: - groupId - A security group id that you can obtain from the AWS Management Console (for example, sg-xxxxxx1c0b65785c3). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val groupId = args[0] - deleteEC2SecGroup(groupId) -} - -// snippet-start:[ec2.kotlin.delete_security_group.main] -suspend fun deleteEC2SecGroup(groupIdVal: String) { - val request = DeleteSecurityGroupRequest { - groupId = groupIdVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.deleteSecurityGroup(request) - println("Successfully deleted Security Group with id $groupIdVal") - } -} -// snippet-end:[ec2.kotlin.delete_security_group.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.delete_security_group.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DeleteSecurityGroupRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.delete_security_group.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + groupId - A security group id that you can obtain from the AWS Management Console (for example, sg-xxxxxx1c0b65785c3). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val groupId = args[0] + deleteEC2SecGroup(groupId) +} + +// snippet-start:[ec2.kotlin.delete_security_group.main] +suspend fun deleteEC2SecGroup(groupIdVal: String) { + val request = + DeleteSecurityGroupRequest { + groupId = groupIdVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.deleteSecurityGroup(request) + println("Successfully deleted Security Group with id $groupIdVal") + } +} +// snippet-end:[ec2.kotlin.delete_security_group.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAccount.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAccount.kt index a90932ae0ff..c41bbc50cb3 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAccount.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAccount.kt @@ -1,35 +1,35 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_account.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeAccountAttributesRequest -// snippet-end:[ec2.kotlin.describe_account.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main() { - describeEC2Account() -} - -// snippet-start:[ec2.kotlin.describe_account.main] -suspend fun describeEC2Account() { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val accountResults = ec2.describeAccountAttributes(DescribeAccountAttributesRequest {}) - accountResults.accountAttributes?.forEach { attribute -> - println("The name of the attribute is ${attribute.attributeName}") - - attribute.attributeValues?.forEach { myValue -> - println("The value of the attribute is ${myValue.attributeValue}") - } - } - } -} -// snippet-end:[ec2.kotlin.describe_account.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_account.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeAccountAttributesRequest +// snippet-end:[ec2.kotlin.describe_account.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main() { + describeEC2Account() +} + +// snippet-start:[ec2.kotlin.describe_account.main] +suspend fun describeEC2Account() { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val accountResults = ec2.describeAccountAttributes(DescribeAccountAttributesRequest {}) + accountResults.accountAttributes?.forEach { attribute -> + println("The name of the attribute is ${attribute.attributeName}") + + attribute.attributeValues?.forEach { myValue -> + println("The value of the attribute is ${myValue.attributeValue}") + } + } + } +} +// snippet-end:[ec2.kotlin.describe_account.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAddresses.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAddresses.kt index c1f01e1535d..9c65a74a313 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAddresses.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeAddresses.kt @@ -1,32 +1,34 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_addresses.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeAddressesRequest -// snippet-end:[ec2.kotlin.describe_addresses.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main() { - describeEC2Address() -} - -// snippet-start:[ec2.kotlin.describe_addresses.main] -suspend fun describeEC2Address() { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeAddresses(DescribeAddressesRequest {}) - response.addresses?.forEach { address -> - println("Found address with public IP ${address.publicIp}, domain is ${address.domain}, allocation id ${address.allocationId} and NIC id: ${address.networkInterfaceId} ") - } - } -} -// snippet-end:[ec2.kotlin.describe_addresses.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_addresses.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeAddressesRequest +// snippet-end:[ec2.kotlin.describe_addresses.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main() { + describeEC2Address() +} + +// snippet-start:[ec2.kotlin.describe_addresses.main] +suspend fun describeEC2Address() { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeAddresses(DescribeAddressesRequest {}) + response.addresses?.forEach { address -> + println( + "Found address with public IP ${address.publicIp}, domain is ${address.domain}, allocation id ${address.allocationId} and NIC id: ${address.networkInterfaceId} ", + ) + } + } +} +// snippet-end:[ec2.kotlin.describe_addresses.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstanceTags.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstanceTags.kt index 3fdd75606d1..481bede38ff 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstanceTags.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstanceTags.kt @@ -1,57 +1,59 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_instances_tags.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeTagsRequest -import aws.sdk.kotlin.services.ec2.model.Filter -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.describe_instances_tags.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - Usage: - - - Where: - resourceIdVal - The instance ID value that you can obtain from the AWS Management Console (for example, i-xxxxxx0913e05f482). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val resourceIdVal = args[0] - describeEC2Tags(resourceIdVal) -} - -// snippet-start:[ec2.kotlin.describe_instances_tags.main] -suspend fun describeEC2Tags(resourceIdVal: String) { - val filter = Filter { - name = "resource-id" - values = listOf(resourceIdVal) - } - - val request = DescribeTagsRequest { - filters = listOf(filter) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeTags(request) - response.tags?.forEach { tag -> - println("Tag key is ${tag.key}") - println("Tag value is ${tag.value}") - } - } -} -// snippet-end:[ec2.kotlin.describe_instances_tags.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_instances_tags.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeTagsRequest +import aws.sdk.kotlin.services.ec2.model.Filter +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.describe_instances_tags.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + resourceIdVal - The instance ID value that you can obtain from the AWS Management Console (for example, i-xxxxxx0913e05f482). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val resourceIdVal = args[0] + describeEC2Tags(resourceIdVal) +} + +// snippet-start:[ec2.kotlin.describe_instances_tags.main] +suspend fun describeEC2Tags(resourceIdVal: String) { + val filter = + Filter { + name = "resource-id" + values = listOf(resourceIdVal) + } + + val request = + DescribeTagsRequest { + filters = listOf(filter) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeTags(request) + response.tags?.forEach { tag -> + println("Tag key is ${tag.key}") + println("Tag value is ${tag.value}") + } + } +} +// snippet-end:[ec2.kotlin.describe_instances_tags.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstances.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstances.kt index f2547cb33e4..9dd57235218 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstances.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeInstances.kt @@ -1,42 +1,43 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_instances.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest -// snippet-end:[ec2.kotlin.describe_instances.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main() { - describeEC2Instances() -} - -// snippet-start:[ec2.kotlin.describe_instances.main] -suspend fun describeEC2Instances() { - val request = DescribeInstancesRequest { - maxResults = 6 - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeInstances(request) - response.reservations?.forEach { reservation -> - reservation.instances?.forEach { instance -> - println("Instance Id is ${instance.instanceId}") - println("Image id is ${instance.imageId}") - println("Instance type is ${instance.instanceType}") - println("Instance state name is ${instance.state?.name}") - println("monitoring information is ${instance.monitoring?.state}") - } - } - } -} -// snippet-end:[ec2.kotlin.describe_instances.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_instances.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest +// snippet-end:[ec2.kotlin.describe_instances.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main() { + describeEC2Instances() +} + +// snippet-start:[ec2.kotlin.describe_instances.main] +suspend fun describeEC2Instances() { + val request = + DescribeInstancesRequest { + maxResults = 6 + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeInstances(request) + response.reservations?.forEach { reservation -> + reservation.instances?.forEach { instance -> + println("Instance Id is ${instance.instanceId}") + println("Image id is ${instance.imageId}") + println("Instance type is ${instance.instanceType}") + println("Instance state name is ${instance.state?.name}") + println("monitoring information is ${instance.monitoring?.state}") + } + } + } +} +// snippet-end:[ec2.kotlin.describe_instances.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeKeyPairs.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeKeyPairs.kt index 894ea6e824e..319a204dd91 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeKeyPairs.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeKeyPairs.kt @@ -1,32 +1,32 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_key_pairs.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeKeyPairsRequest -// snippet-end:[ec2.kotlin.describe_key_pairs.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main() { - describeEC2Keys() -} - -// snippet-start:[ec2.kotlin.describe_key_pairs.main] -suspend fun describeEC2Keys() { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeKeyPairs(DescribeKeyPairsRequest {}) - response.keyPairs?.forEach { keyPair -> - println("Found key pair with name ${keyPair.keyName} and fingerprint ${ keyPair.keyFingerprint}") - } - } -} -// snippet-end:[ec2.kotlin.describe_key_pairs.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_key_pairs.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeKeyPairsRequest +// snippet-end:[ec2.kotlin.describe_key_pairs.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main() { + describeEC2Keys() +} + +// snippet-start:[ec2.kotlin.describe_key_pairs.main] +suspend fun describeEC2Keys() { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeKeyPairs(DescribeKeyPairsRequest {}) + response.keyPairs?.forEach { keyPair -> + println("Found key pair with name ${keyPair.keyName} and fingerprint ${ keyPair.keyFingerprint}") + } + } +} +// snippet-end:[ec2.kotlin.describe_key_pairs.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeRegionsAndZones.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeRegionsAndZones.kt index 30bb40d2a78..95cd85642ad 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeRegionsAndZones.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeRegionsAndZones.kt @@ -1,38 +1,38 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_region_and_zones.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeAvailabilityZonesRequest -import aws.sdk.kotlin.services.ec2.model.DescribeRegionsRequest -// snippet-end:[ec2.kotlin.describe_region_and_zones.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main() { - describeEC2RegionsAndZones() -} - -// snippet-start:[ec2.kotlin.describe_region_and_zones.main] -suspend fun describeEC2RegionsAndZones() { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val regionsResponse = ec2.describeRegions(DescribeRegionsRequest {}) - regionsResponse.regions?.forEach { region -> - println("Found Region ${region.regionName} with endpoint ${region.endpoint}") - } - - val zonesResponse = ec2.describeAvailabilityZones(DescribeAvailabilityZonesRequest {}) - zonesResponse.availabilityZones?.forEach { zone -> - println("Found Availability Zone ${zone.zoneName} with status ${zone.state} in Region ${zone.regionName}") - } - } -} -// snippet-end:[ec2.kotlin.describe_region_and_zones.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_region_and_zones.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeAvailabilityZonesRequest +import aws.sdk.kotlin.services.ec2.model.DescribeRegionsRequest +// snippet-end:[ec2.kotlin.describe_region_and_zones.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main() { + describeEC2RegionsAndZones() +} + +// snippet-start:[ec2.kotlin.describe_region_and_zones.main] +suspend fun describeEC2RegionsAndZones() { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val regionsResponse = ec2.describeRegions(DescribeRegionsRequest {}) + regionsResponse.regions?.forEach { region -> + println("Found Region ${region.regionName} with endpoint ${region.endpoint}") + } + + val zonesResponse = ec2.describeAvailabilityZones(DescribeAvailabilityZonesRequest {}) + zonesResponse.availabilityZones?.forEach { zone -> + println("Found Availability Zone ${zone.zoneName} with status ${zone.state} in Region ${zone.regionName}") + } + } +} +// snippet-end:[ec2.kotlin.describe_region_and_zones.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeSecurityGroups.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeSecurityGroups.kt index d8a1939da6f..95d356eae11 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeSecurityGroups.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeSecurityGroups.kt @@ -1,53 +1,54 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_security_groups.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeSecurityGroupsRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.describe_security_groups.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - groupId - a security group id. - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val groupId = args[0] - describeEC2SecurityGroups(groupId) -} - -// snippet-start:[ec2.kotlin.describe_security_groups.main] -suspend fun describeEC2SecurityGroups(groupId: String) { - val request = DescribeSecurityGroupsRequest { - groupIds = listOf(groupId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - - val response = ec2.describeSecurityGroups(request) - response.securityGroups?.forEach { group -> - println("Found Security Group with id ${group.groupId}, vpc id ${group.vpcId} and description ${group.description}") - } - } -} -// snippet-end:[ec2.kotlin.describe_security_groups.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_security_groups.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeSecurityGroupsRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.describe_security_groups.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + groupId - a security group id. + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val groupId = args[0] + describeEC2SecurityGroups(groupId) +} + +// snippet-start:[ec2.kotlin.describe_security_groups.main] +suspend fun describeEC2SecurityGroups(groupId: String) { + val request = + DescribeSecurityGroupsRequest { + groupIds = listOf(groupId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + + val response = ec2.describeSecurityGroups(request) + response.securityGroups?.forEach { group -> + println("Found Security Group with id ${group.groupId}, vpc id ${group.vpcId} and description ${group.description}") + } + } +} +// snippet-end:[ec2.kotlin.describe_security_groups.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeVPCs.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeVPCs.kt index e1b0e364ced..9f1c6d311f4 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeVPCs.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/DescribeVPCs.kt @@ -1,52 +1,53 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.describe_vpc.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeVpcsRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.describe_vpc.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - vpcId - A VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f). - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val vpcId = args[0] - describeEC2Vpcs(vpcId) -} - -// snippet-start:[ec2.kotlin.describe_vpc.main] -suspend fun describeEC2Vpcs(vpcId: String) { - val request = DescribeVpcsRequest { - vpcIds = listOf(vpcId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeVpcs(request) - response.vpcs?.forEach { vpc -> - println("Found VPC with id ${vpc.vpcId} VPC state ${vpc.state} and tenancy ${vpc.instanceTenancy}") - } - } -} -// snippet-end:[ec2.kotlin.describe_vpc.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.describe_vpc.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeVpcsRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.describe_vpc.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + vpcId - A VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f). + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val vpcId = args[0] + describeEC2Vpcs(vpcId) +} + +// snippet-start:[ec2.kotlin.describe_vpc.main] +suspend fun describeEC2Vpcs(vpcId: String) { + val request = + DescribeVpcsRequest { + vpcIds = listOf(vpcId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeVpcs(request) + response.vpcs?.forEach { vpc -> + println("Found VPC with id ${vpc.vpcId} VPC state ${vpc.state} and tenancy ${vpc.instanceTenancy}") + } + } +} +// snippet-end:[ec2.kotlin.describe_vpc.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/EC2Scenario.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/EC2Scenario.kt index 4ca2baabd95..665ade8d7e6 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/EC2Scenario.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/EC2Scenario.kt @@ -1,527 +1,583 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.AllocateAddressRequest -import aws.sdk.kotlin.services.ec2.model.AssociateAddressRequest -import aws.sdk.kotlin.services.ec2.model.AuthorizeSecurityGroupIngressRequest -import aws.sdk.kotlin.services.ec2.model.CreateKeyPairRequest -import aws.sdk.kotlin.services.ec2.model.CreateSecurityGroupRequest -import aws.sdk.kotlin.services.ec2.model.DeleteKeyPairRequest -import aws.sdk.kotlin.services.ec2.model.DeleteSecurityGroupRequest -import aws.sdk.kotlin.services.ec2.model.DescribeImagesRequest -import aws.sdk.kotlin.services.ec2.model.DescribeInstanceTypesRequest -import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest -import aws.sdk.kotlin.services.ec2.model.DescribeKeyPairsRequest -import aws.sdk.kotlin.services.ec2.model.DescribeSecurityGroupsRequest -import aws.sdk.kotlin.services.ec2.model.DisassociateAddressRequest -import aws.sdk.kotlin.services.ec2.model.DomainType -import aws.sdk.kotlin.services.ec2.model.Filter -import aws.sdk.kotlin.services.ec2.model.InstanceType -import aws.sdk.kotlin.services.ec2.model.IpPermission -import aws.sdk.kotlin.services.ec2.model.IpRange -import aws.sdk.kotlin.services.ec2.model.ReleaseAddressRequest -import aws.sdk.kotlin.services.ec2.model.RunInstancesRequest -import aws.sdk.kotlin.services.ec2.model.StartInstancesRequest -import aws.sdk.kotlin.services.ec2.model.StopInstancesRequest -import aws.sdk.kotlin.services.ec2.model.TerminateInstancesRequest -import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceRunning -import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceStopped -import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceTerminated -import aws.sdk.kotlin.services.ssm.SsmClient -import aws.sdk.kotlin.services.ssm.model.GetParametersByPathRequest -import java.io.File -import kotlin.system.exitProcess - -// snippet-start:[ec2.kotlin.scenario.main] -/** - Before running this Kotlin code example, set up your development environment, - including your credentials. - - For more information, see the following documentation topic: - https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - - This Kotlin example performs the following tasks: - - 1. Creates an RSA key pair and saves the private key data as a .pem file. - 2. Lists key pairs. - 3. Creates a security group for the default VPC. - 4. Displays security group information. - 5. Gets a list of Amazon Linux 2 AMIs and selects one. - 6. Gets more information about the image. - 7. Gets a list of instance types that are compatible with the selected AMI’s architecture. - 8. Creates an instance with the key pair, security group, AMI, and an instance type. - 9. Displays information about the instance. - 10. Stops the instance and waits for it to stop. - 11. Starts the instance and waits for it to start. - 12. Allocates an Elastic IP address and associates it with the instance. - 13. Displays SSH connection info for the instance. - 14. Disassociates and deletes the Elastic IP address. - 15. Terminates the instance. - 16. Deletes the security group. - 17. Deletes the key pair. - */ - -val DASHES = String(CharArray(80)).replace("\u0000", "-") -suspend fun main(args: Array) { - val usage = """ - Usage: - - - Where: - keyName - A key pair name (for example, TestKeyPair). - fileName - A file name where the key information is written to. - groupName - The name of the security group. - groupDesc - The description of the security group. - vpcId - A VPC ID. You can get this value from the AWS Management Console. - myIpAddress - The IP address of your development machine. - -""" - - if (args.size != 6) { - println(usage) - exitProcess(0) - } - - val keyName = args[0] - val fileName = args[1] - val groupName = args[2] - val groupDesc = args[3] - val vpcId = args[4] - val myIpAddress = args[5] - var newInstanceId: String? = "" - - println(DASHES) - println("Welcome to the Amazon EC2 example scenario.") - println(DASHES) - - println(DASHES) - println("1. Create an RSA key pair and save the private key material as a .pem file.") - createKeyPairSc(keyName, fileName) - println(DASHES) - - println(DASHES) - println("2. List key pairs.") - describeEC2KeysSc() - println(DASHES) - - println(DASHES) - println("3. Create a security group.") - val groupId = createEC2SecurityGroupSc(groupName, groupDesc, vpcId, myIpAddress) - println(DASHES) - - println(DASHES) - println("4. Display security group info for the newly created security group.") - describeSecurityGroupsSc(groupId.toString()) - println(DASHES) - - println(DASHES) - println("5. Get a list of Amazon Linux 2 AMIs and select one with amzn2 in the name.") - val instanceId = getParaValuesSc() - if (instanceId == "") { - println("The instance Id value isn't valid.") - exitProcess(0) - } - println("The instance Id is $instanceId.") - println(DASHES) - - println(DASHES) - println("6. Get more information about an amzn2 image and return the AMI value.") - val amiValue = instanceId?.let { describeImageSc(it) } - if (instanceId == "") { - println("The instance Id value is invalid.") - exitProcess(0) - } - println("The AMI value is $amiValue.") - println(DASHES) - - println(DASHES) - println("7. Get a list of instance types.") - val instanceType = getInstanceTypesSc() - println(DASHES) - - println(DASHES) - println("8. Create an instance.") - if (amiValue != null) { - newInstanceId = runInstanceSc(instanceType, keyName, groupName, amiValue) - println("The instance Id is $newInstanceId") - } - println(DASHES) - - println(DASHES) - println("9. Display information about the running instance. ") - var ipAddress = describeEC2InstancesSc(newInstanceId) - println("You can SSH to the instance using this command:") - println("ssh -i " + fileName + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("10. Stop the instance.") - if (newInstanceId != null) { - stopInstanceSc(newInstanceId) - } - println(DASHES) - - println(DASHES) - println("11. Start the instance.") - if (newInstanceId != null) { - startInstanceSc(newInstanceId) - } - ipAddress = describeEC2InstancesSc(newInstanceId) - println("You can SSH to the instance using this command:") - println("ssh -i " + fileName + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("12. Allocate an Elastic IP address and associate it with the instance.") - val allocationId = allocateAddressSc() - println("The allocation Id value is $allocationId") - val associationId = associateAddressSc(newInstanceId, allocationId) - println("The associate Id value is $associationId") - println(DASHES) - - println(DASHES) - println("13. Describe the instance again.") - ipAddress = describeEC2InstancesSc(newInstanceId) - println("You can SSH to the instance using this command:") - println("ssh -i " + fileName + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("14. Disassociate and release the Elastic IP address.") - disassociateAddressSc(associationId) - releaseEC2AddressSc(allocationId) - println(DASHES) - - println(DASHES) - println("15. Terminate the instance and use a waiter.") - if (newInstanceId != null) { - terminateEC2Sc(newInstanceId) - } - println(DASHES) - - println(DASHES) - println("16. Delete the security group.") - if (groupId != null) { - deleteEC2SecGroupSc(groupId) - } - println(DASHES) - - println(DASHES) - println("17. Delete the key pair.") - deleteKeysSc(keyName) - println(DASHES) - - println(DASHES) - println("You successfully completed the Amazon EC2 scenario.") - println(DASHES) -} - -suspend fun deleteKeysSc(keyPair: String) { - val request = DeleteKeyPairRequest { - keyName = keyPair - } - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.deleteKeyPair(request) - println("Successfully deleted key pair named $keyPair") - } -} - -suspend fun deleteEC2SecGroupSc(groupIdVal: String) { - val request = DeleteSecurityGroupRequest { - groupId = groupIdVal - } - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.deleteSecurityGroup(request) - println("Successfully deleted security group with Id $groupIdVal") - } -} - -suspend fun terminateEC2Sc(instanceIdVal: String) { - val ti = TerminateInstancesRequest { - instanceIds = listOf(instanceIdVal) - } - println("Wait for the instance to terminate. This will take a few minutes.") - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.terminateInstances(ti) - ec2.waitUntilInstanceTerminated { // suspend call - instanceIds = listOf(instanceIdVal) - } - println("$instanceIdVal is terminated!") - } -} - -// snippet-start:[ec2.kotlin.scenario.release.address.main] -suspend fun releaseEC2AddressSc(allocId: String?) { - val request = ReleaseAddressRequest { - allocationId = allocId - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.releaseAddress(request) - println("Successfully released Elastic IP address $allocId") - } -} -// snippet-end:[ec2.kotlin.scenario.release.address.main] - -// snippet-start:[ec2.kotlin.scenario.disassociate_address.main] -suspend fun disassociateAddressSc(associationIdVal: String?) { - val addressRequest = DisassociateAddressRequest { - associationId = associationIdVal - } - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.disassociateAddress(addressRequest) - println("You successfully disassociated the address!") - } -} -// snippet-end:[ec2.kotlin.scenario.disassociate_address.main] - -// snippet-start:[ec2.kotlin.associate_address.main] -suspend fun associateAddressSc(instanceIdVal: String?, allocationIdVal: String?): String? { - val associateRequest = AssociateAddressRequest { - instanceId = instanceIdVal - allocationId = allocationIdVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val associateResponse = ec2.associateAddress(associateRequest) - return associateResponse.associationId - } -} -// snippet-end:[ec2.kotlin.associate_address.main] - -suspend fun allocateAddressSc(): String? { - val allocateRequest = AllocateAddressRequest { - domain = DomainType.Vpc - } - Ec2Client { region = "us-west-2" }.use { ec2 -> - val allocateResponse = ec2.allocateAddress(allocateRequest) - return allocateResponse.allocationId - } -} - -// snippet-start:[ec2.kotlin.scenario.start_instance.main] -suspend fun startInstanceSc(instanceId: String) { - val request = StartInstancesRequest { - instanceIds = listOf(instanceId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.startInstances(request) - println("Waiting until instance $instanceId starts. This will take a few minutes.") - ec2.waitUntilInstanceRunning { // suspend call - instanceIds = listOf(instanceId) - } - println("Successfully started instance $instanceId") - } -} -// snippet-end:[ec2.kotlin.scenario.start_instance.main] - -// snippet-start:[ec2.kotlin.scenario.stop_instance.main] -suspend fun stopInstanceSc(instanceId: String) { - val request = StopInstancesRequest { - instanceIds = listOf(instanceId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - ec2.stopInstances(request) - println("Waiting until instance $instanceId stops. This will take a few minutes.") - ec2.waitUntilInstanceStopped { // suspend call - instanceIds = listOf(instanceId) - } - println("Successfully stopped instance $instanceId") - } -} -// snippet-end:[ec2.kotlin.scenario.stop_instance.main] - -suspend fun describeEC2InstancesSc(newInstanceId: String?): String { - var pubAddress = "" - var isRunning = false - val request = DescribeInstancesRequest { - instanceIds = listOf(newInstanceId.toString()) - } - - while (!isRunning) { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeInstances(request) - val state = response.reservations?.get(0)?.instances?.get(0)?.state?.name?. value - if (state != null) { - if (state.compareTo("running") == 0) { - println("Image id is ${response.reservations!!.get(0).instances?.get(0)?.imageId}") - println("Instance type is ${response.reservations!!.get(0).instances?.get(0)?.instanceType}") - println("Instance state is ${response.reservations!!.get(0).instances?.get(0)?.state}") - pubAddress = response.reservations!!.get(0).instances?.get(0)?.publicIpAddress.toString() - println("Instance address is $pubAddress") - isRunning = true - } - } - } - } - return pubAddress -} - -suspend fun runInstanceSc(instanceTypeVal: String, keyNameVal: String, groupNameVal: String, amiIdVal: String): String { - val runRequest = RunInstancesRequest { - instanceType = InstanceType.fromValue(instanceTypeVal) - keyName = keyNameVal - securityGroups = listOf(groupNameVal) - maxCount = 1 - minCount = 1 - imageId = amiIdVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.runInstances(runRequest) - val instanceId = response.instances?.get(0)?.instanceId - println("Successfully started EC2 Instance $instanceId based on AMI $amiIdVal") - return instanceId.toString() - } -} - -// snippet-start:[ec2.kotlin.describe_instance_type.main] -// Get a list of instance types. -suspend fun getInstanceTypesSc(): String { - var instanceType = "" - val filterObs = ArrayList() - val filter = Filter { - name = "processor-info.supported-architecture" - values = listOf("arm64") - } - - filterObs.add(filter) - val typesRequest = DescribeInstanceTypesRequest { - filters = filterObs - maxResults = 10 - } - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeInstanceTypes(typesRequest) - response.instanceTypes?.forEach { type -> - println("The memory information of this type is ${type.memoryInfo?.sizeInMib}") - println("Maximum number of network cards is ${type.networkInfo?.maximumNetworkCards}") - instanceType = type.instanceType.toString() - } - return instanceType - } -} -// snippet-end:[ec2.kotlin.describe_instance_type.main] - -// Display the Description field that corresponds to the instance Id value. -suspend fun describeImageSc(instanceId: String): String? { - val imagesRequest = DescribeImagesRequest { - imageIds = listOf(instanceId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeImages(imagesRequest) - println("The description of the first image is ${response.images?.get(0)?.description}") - println("The name of the first image is ${response.images?.get(0)?.name}") - - // Return the image Id value. - return response.images?.get(0)?.imageId - } -} - -// Get the Id value of an instance with amzn2 in the name. -suspend fun getParaValuesSc(): String? { - val parameterRequest = GetParametersByPathRequest { - path = "/aws/service/ami-amazon-linux-latest" - } - - SsmClient { region = "us-west-2" }.use { ssmClient -> - val response = ssmClient.getParametersByPath(parameterRequest) - response.parameters?.forEach { para -> - println("The name of the para is: ${para.name}") - println("The type of the para is: ${para.type}") - println("") - if (para.name?.let { filterName(it) } == true) { - return para.value - } - } - } - return "" -} - -fun filterName(name: String): Boolean { - val parts = name.split("/").toTypedArray() - val myValue = parts[4] - return myValue.contains("amzn2") -} - -suspend fun describeSecurityGroupsSc(groupId: String) { - val request = DescribeSecurityGroupsRequest { - groupIds = listOf(groupId) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeSecurityGroups(request) - for (group in response.securityGroups!!) { - println("Found Security Group with id " + group.groupId.toString() + " and group VPC " + group.vpcId) - } - } -} - -// snippet-start:[ec2.kotlin.scenario.inbound.rule.main] -suspend fun createEC2SecurityGroupSc(groupNameVal: String?, groupDescVal: String?, vpcIdVal: String?, myIpAddress: String?): String? { - val request = CreateSecurityGroupRequest { - groupName = groupNameVal - description = groupDescVal - vpcId = vpcIdVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val resp = ec2.createSecurityGroup(request) - val ipRange = IpRange { - cidrIp = "$myIpAddress/0" - } - - val ipPerm = IpPermission { - ipProtocol = "tcp" - toPort = 80 - fromPort = 80 - ipRanges = listOf(ipRange) - } - - val ipPerm2 = IpPermission { - ipProtocol = "tcp" - toPort = 22 - fromPort = 22 - ipRanges = listOf(ipRange) - } - - val authRequest = AuthorizeSecurityGroupIngressRequest { - groupName = groupNameVal - ipPermissions = listOf(ipPerm, ipPerm2) - } - ec2.authorizeSecurityGroupIngress(authRequest) - println("Successfully added ingress policy to Security Group $groupNameVal") - return resp.groupId - } -} -// snippet-end:[ec2.kotlin.scenario.inbound.rule.main] - -suspend fun describeEC2KeysSc() { - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeKeyPairs(DescribeKeyPairsRequest {}) - response.keyPairs?.forEach { keyPair -> - println("Found key pair with name ${keyPair.keyName} and fingerprint ${ keyPair.keyFingerprint}") - } - } -} - -suspend fun createKeyPairSc(keyNameVal: String, fileNameVal: String) { - val request = CreateKeyPairRequest { - keyName = keyNameVal - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.createKeyPair(request) - val content = response.keyMaterial - if (content != null) { - File(fileNameVal).writeText(content) - } - println("Successfully created key pair named $keyNameVal") - } -} -// snippet-end:[ec2.kotlin.scenario.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.AllocateAddressRequest +import aws.sdk.kotlin.services.ec2.model.AssociateAddressRequest +import aws.sdk.kotlin.services.ec2.model.AuthorizeSecurityGroupIngressRequest +import aws.sdk.kotlin.services.ec2.model.CreateKeyPairRequest +import aws.sdk.kotlin.services.ec2.model.CreateSecurityGroupRequest +import aws.sdk.kotlin.services.ec2.model.DeleteKeyPairRequest +import aws.sdk.kotlin.services.ec2.model.DeleteSecurityGroupRequest +import aws.sdk.kotlin.services.ec2.model.DescribeImagesRequest +import aws.sdk.kotlin.services.ec2.model.DescribeInstanceTypesRequest +import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest +import aws.sdk.kotlin.services.ec2.model.DescribeKeyPairsRequest +import aws.sdk.kotlin.services.ec2.model.DescribeSecurityGroupsRequest +import aws.sdk.kotlin.services.ec2.model.DisassociateAddressRequest +import aws.sdk.kotlin.services.ec2.model.DomainType +import aws.sdk.kotlin.services.ec2.model.Filter +import aws.sdk.kotlin.services.ec2.model.InstanceType +import aws.sdk.kotlin.services.ec2.model.IpPermission +import aws.sdk.kotlin.services.ec2.model.IpRange +import aws.sdk.kotlin.services.ec2.model.ReleaseAddressRequest +import aws.sdk.kotlin.services.ec2.model.RunInstancesRequest +import aws.sdk.kotlin.services.ec2.model.StartInstancesRequest +import aws.sdk.kotlin.services.ec2.model.StopInstancesRequest +import aws.sdk.kotlin.services.ec2.model.TerminateInstancesRequest +import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceRunning +import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceStopped +import aws.sdk.kotlin.services.ec2.waiters.waitUntilInstanceTerminated +import aws.sdk.kotlin.services.ssm.SsmClient +import aws.sdk.kotlin.services.ssm.model.GetParametersByPathRequest +import java.io.File +import kotlin.system.exitProcess + +// snippet-start:[ec2.kotlin.scenario.main] + +/** + Before running this Kotlin code example, set up your development environment, + including your credentials. + + For more information, see the following documentation topic: + https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + + This Kotlin example performs the following tasks: + + 1. Creates an RSA key pair and saves the private key data as a .pem file. + 2. Lists key pairs. + 3. Creates a security group for the default VPC. + 4. Displays security group information. + 5. Gets a list of Amazon Linux 2 AMIs and selects one. + 6. Gets more information about the image. + 7. Gets a list of instance types that are compatible with the selected AMI’s architecture. + 8. Creates an instance with the key pair, security group, AMI, and an instance type. + 9. Displays information about the instance. + 10. Stops the instance and waits for it to stop. + 11. Starts the instance and waits for it to start. + 12. Allocates an Elastic IP address and associates it with the instance. + 13. Displays SSH connection info for the instance. + 14. Disassociates and deletes the Elastic IP address. + 15. Terminates the instance. + 16. Deletes the security group. + 17. Deletes the key pair. + */ + +val DASHES = String(CharArray(80)).replace("\u0000", "-") + +suspend fun main(args: Array) { + val usage = """ + Usage: + + + Where: + keyName - A key pair name (for example, TestKeyPair). + fileName - A file name where the key information is written to. + groupName - The name of the security group. + groupDesc - The description of the security group. + vpcId - A VPC ID. You can get this value from the AWS Management Console. + myIpAddress - The IP address of your development machine. + +""" + + if (args.size != 6) { + println(usage) + exitProcess(0) + } + + val keyName = args[0] + val fileName = args[1] + val groupName = args[2] + val groupDesc = args[3] + val vpcId = args[4] + val myIpAddress = args[5] + var newInstanceId: String? = "" + + println(DASHES) + println("Welcome to the Amazon EC2 example scenario.") + println(DASHES) + + println(DASHES) + println("1. Create an RSA key pair and save the private key material as a .pem file.") + createKeyPairSc(keyName, fileName) + println(DASHES) + + println(DASHES) + println("2. List key pairs.") + describeEC2KeysSc() + println(DASHES) + + println(DASHES) + println("3. Create a security group.") + val groupId = createEC2SecurityGroupSc(groupName, groupDesc, vpcId, myIpAddress) + println(DASHES) + + println(DASHES) + println("4. Display security group info for the newly created security group.") + describeSecurityGroupsSc(groupId.toString()) + println(DASHES) + + println(DASHES) + println("5. Get a list of Amazon Linux 2 AMIs and select one with amzn2 in the name.") + val instanceId = getParaValuesSc() + if (instanceId == "") { + println("The instance Id value isn't valid.") + exitProcess(0) + } + println("The instance Id is $instanceId.") + println(DASHES) + + println(DASHES) + println("6. Get more information about an amzn2 image and return the AMI value.") + val amiValue = instanceId?.let { describeImageSc(it) } + if (instanceId == "") { + println("The instance Id value is invalid.") + exitProcess(0) + } + println("The AMI value is $amiValue.") + println(DASHES) + + println(DASHES) + println("7. Get a list of instance types.") + val instanceType = getInstanceTypesSc() + println(DASHES) + + println(DASHES) + println("8. Create an instance.") + if (amiValue != null) { + newInstanceId = runInstanceSc(instanceType, keyName, groupName, amiValue) + println("The instance Id is $newInstanceId") + } + println(DASHES) + + println(DASHES) + println("9. Display information about the running instance. ") + var ipAddress = describeEC2InstancesSc(newInstanceId) + println("You can SSH to the instance using this command:") + println("ssh -i " + fileName + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("10. Stop the instance.") + if (newInstanceId != null) { + stopInstanceSc(newInstanceId) + } + println(DASHES) + + println(DASHES) + println("11. Start the instance.") + if (newInstanceId != null) { + startInstanceSc(newInstanceId) + } + ipAddress = describeEC2InstancesSc(newInstanceId) + println("You can SSH to the instance using this command:") + println("ssh -i " + fileName + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("12. Allocate an Elastic IP address and associate it with the instance.") + val allocationId = allocateAddressSc() + println("The allocation Id value is $allocationId") + val associationId = associateAddressSc(newInstanceId, allocationId) + println("The associate Id value is $associationId") + println(DASHES) + + println(DASHES) + println("13. Describe the instance again.") + ipAddress = describeEC2InstancesSc(newInstanceId) + println("You can SSH to the instance using this command:") + println("ssh -i " + fileName + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("14. Disassociate and release the Elastic IP address.") + disassociateAddressSc(associationId) + releaseEC2AddressSc(allocationId) + println(DASHES) + + println(DASHES) + println("15. Terminate the instance and use a waiter.") + if (newInstanceId != null) { + terminateEC2Sc(newInstanceId) + } + println(DASHES) + + println(DASHES) + println("16. Delete the security group.") + if (groupId != null) { + deleteEC2SecGroupSc(groupId) + } + println(DASHES) + + println(DASHES) + println("17. Delete the key pair.") + deleteKeysSc(keyName) + println(DASHES) + + println(DASHES) + println("You successfully completed the Amazon EC2 scenario.") + println(DASHES) +} + +suspend fun deleteKeysSc(keyPair: String) { + val request = + DeleteKeyPairRequest { + keyName = keyPair + } + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.deleteKeyPair(request) + println("Successfully deleted key pair named $keyPair") + } +} + +suspend fun deleteEC2SecGroupSc(groupIdVal: String) { + val request = + DeleteSecurityGroupRequest { + groupId = groupIdVal + } + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.deleteSecurityGroup(request) + println("Successfully deleted security group with Id $groupIdVal") + } +} + +suspend fun terminateEC2Sc(instanceIdVal: String) { + val ti = + TerminateInstancesRequest { + instanceIds = listOf(instanceIdVal) + } + println("Wait for the instance to terminate. This will take a few minutes.") + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.terminateInstances(ti) + ec2.waitUntilInstanceTerminated { + // suspend call + instanceIds = listOf(instanceIdVal) + } + println("$instanceIdVal is terminated!") + } +} + +// snippet-start:[ec2.kotlin.scenario.release.address.main] +suspend fun releaseEC2AddressSc(allocId: String?) { + val request = + ReleaseAddressRequest { + allocationId = allocId + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.releaseAddress(request) + println("Successfully released Elastic IP address $allocId") + } +} +// snippet-end:[ec2.kotlin.scenario.release.address.main] + +// snippet-start:[ec2.kotlin.scenario.disassociate_address.main] +suspend fun disassociateAddressSc(associationIdVal: String?) { + val addressRequest = + DisassociateAddressRequest { + associationId = associationIdVal + } + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.disassociateAddress(addressRequest) + println("You successfully disassociated the address!") + } +} +// snippet-end:[ec2.kotlin.scenario.disassociate_address.main] + +// snippet-start:[ec2.kotlin.associate_address.main] +suspend fun associateAddressSc( + instanceIdVal: String?, + allocationIdVal: String?, +): String? { + val associateRequest = + AssociateAddressRequest { + instanceId = instanceIdVal + allocationId = allocationIdVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val associateResponse = ec2.associateAddress(associateRequest) + return associateResponse.associationId + } +} +// snippet-end:[ec2.kotlin.associate_address.main] + +suspend fun allocateAddressSc(): String? { + val allocateRequest = + AllocateAddressRequest { + domain = DomainType.Vpc + } + Ec2Client { region = "us-west-2" }.use { ec2 -> + val allocateResponse = ec2.allocateAddress(allocateRequest) + return allocateResponse.allocationId + } +} + +// snippet-start:[ec2.kotlin.scenario.start_instance.main] +suspend fun startInstanceSc(instanceId: String) { + val request = + StartInstancesRequest { + instanceIds = listOf(instanceId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.startInstances(request) + println("Waiting until instance $instanceId starts. This will take a few minutes.") + ec2.waitUntilInstanceRunning { + // suspend call + instanceIds = listOf(instanceId) + } + println("Successfully started instance $instanceId") + } +} +// snippet-end:[ec2.kotlin.scenario.start_instance.main] + +// snippet-start:[ec2.kotlin.scenario.stop_instance.main] +suspend fun stopInstanceSc(instanceId: String) { + val request = + StopInstancesRequest { + instanceIds = listOf(instanceId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + ec2.stopInstances(request) + println("Waiting until instance $instanceId stops. This will take a few minutes.") + ec2.waitUntilInstanceStopped { + // suspend call + instanceIds = listOf(instanceId) + } + println("Successfully stopped instance $instanceId") + } +} +// snippet-end:[ec2.kotlin.scenario.stop_instance.main] + +suspend fun describeEC2InstancesSc(newInstanceId: String?): String { + var pubAddress = "" + var isRunning = false + val request = + DescribeInstancesRequest { + instanceIds = listOf(newInstanceId.toString()) + } + + while (!isRunning) { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeInstances(request) + val state = + response.reservations + ?.get(0) + ?.instances + ?.get(0) + ?.state + ?.name + ?. value + if (state != null) { + if (state.compareTo("running") == 0) { + println("Image id is ${response.reservations!!.get(0).instances?.get(0)?.imageId}") + println("Instance type is ${response.reservations!!.get(0).instances?.get(0)?.instanceType}") + println("Instance state is ${response.reservations!!.get(0).instances?.get(0)?.state}") + pubAddress = + response.reservations!! + .get(0) + .instances + ?.get(0) + ?.publicIpAddress + .toString() + println("Instance address is $pubAddress") + isRunning = true + } + } + } + } + return pubAddress +} + +suspend fun runInstanceSc( + instanceTypeVal: String, + keyNameVal: String, + groupNameVal: String, + amiIdVal: String, +): String { + val runRequest = + RunInstancesRequest { + instanceType = InstanceType.fromValue(instanceTypeVal) + keyName = keyNameVal + securityGroups = listOf(groupNameVal) + maxCount = 1 + minCount = 1 + imageId = amiIdVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.runInstances(runRequest) + val instanceId = response.instances?.get(0)?.instanceId + println("Successfully started EC2 Instance $instanceId based on AMI $amiIdVal") + return instanceId.toString() + } +} + +// snippet-start:[ec2.kotlin.describe_instance_type.main] +// Get a list of instance types. +suspend fun getInstanceTypesSc(): String { + var instanceType = "" + val filterObs = ArrayList() + val filter = + Filter { + name = "processor-info.supported-architecture" + values = listOf("arm64") + } + + filterObs.add(filter) + val typesRequest = + DescribeInstanceTypesRequest { + filters = filterObs + maxResults = 10 + } + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeInstanceTypes(typesRequest) + response.instanceTypes?.forEach { type -> + println("The memory information of this type is ${type.memoryInfo?.sizeInMib}") + println("Maximum number of network cards is ${type.networkInfo?.maximumNetworkCards}") + instanceType = type.instanceType.toString() + } + return instanceType + } +} +// snippet-end:[ec2.kotlin.describe_instance_type.main] + +// Display the Description field that corresponds to the instance Id value. +suspend fun describeImageSc(instanceId: String): String? { + val imagesRequest = + DescribeImagesRequest { + imageIds = listOf(instanceId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeImages(imagesRequest) + println("The description of the first image is ${response.images?.get(0)?.description}") + println("The name of the first image is ${response.images?.get(0)?.name}") + + // Return the image Id value. + return response.images?.get(0)?.imageId + } +} + +// Get the Id value of an instance with amzn2 in the name. +suspend fun getParaValuesSc(): String? { + val parameterRequest = + GetParametersByPathRequest { + path = "/aws/service/ami-amazon-linux-latest" + } + + SsmClient { region = "us-west-2" }.use { ssmClient -> + val response = ssmClient.getParametersByPath(parameterRequest) + response.parameters?.forEach { para -> + println("The name of the para is: ${para.name}") + println("The type of the para is: ${para.type}") + println("") + if (para.name?.let { filterName(it) } == true) { + return para.value + } + } + } + return "" +} + +fun filterName(name: String): Boolean { + val parts = name.split("/").toTypedArray() + val myValue = parts[4] + return myValue.contains("amzn2") +} + +suspend fun describeSecurityGroupsSc(groupId: String) { + val request = + DescribeSecurityGroupsRequest { + groupIds = listOf(groupId) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeSecurityGroups(request) + for (group in response.securityGroups!!) { + println("Found Security Group with id " + group.groupId.toString() + " and group VPC " + group.vpcId) + } + } +} + +// snippet-start:[ec2.kotlin.scenario.inbound.rule.main] +suspend fun createEC2SecurityGroupSc( + groupNameVal: String?, + groupDescVal: String?, + vpcIdVal: String?, + myIpAddress: String?, +): String? { + val request = + CreateSecurityGroupRequest { + groupName = groupNameVal + description = groupDescVal + vpcId = vpcIdVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val resp = ec2.createSecurityGroup(request) + val ipRange = + IpRange { + cidrIp = "$myIpAddress/0" + } + + val ipPerm = + IpPermission { + ipProtocol = "tcp" + toPort = 80 + fromPort = 80 + ipRanges = listOf(ipRange) + } + + val ipPerm2 = + IpPermission { + ipProtocol = "tcp" + toPort = 22 + fromPort = 22 + ipRanges = listOf(ipRange) + } + + val authRequest = + AuthorizeSecurityGroupIngressRequest { + groupName = groupNameVal + ipPermissions = listOf(ipPerm, ipPerm2) + } + ec2.authorizeSecurityGroupIngress(authRequest) + println("Successfully added ingress policy to Security Group $groupNameVal") + return resp.groupId + } +} +// snippet-end:[ec2.kotlin.scenario.inbound.rule.main] + +suspend fun describeEC2KeysSc() { + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeKeyPairs(DescribeKeyPairsRequest {}) + response.keyPairs?.forEach { keyPair -> + println("Found key pair with name ${keyPair.keyName} and fingerprint ${ keyPair.keyFingerprint}") + } + } +} + +suspend fun createKeyPairSc( + keyNameVal: String, + fileNameVal: String, +) { + val request = + CreateKeyPairRequest { + keyName = keyNameVal + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.createKeyPair(request) + val content = response.keyMaterial + if (content != null) { + File(fileNameVal).writeText(content) + } + println("Successfully created key pair named $keyNameVal") + } +} +// snippet-end:[ec2.kotlin.scenario.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/FindRunningInstances.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/FindRunningInstances.kt index c80d2fa532f..4b9a8fbaeb7 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/FindRunningInstances.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/FindRunningInstances.kt @@ -1,43 +1,47 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.running_instances.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest -import aws.sdk.kotlin.services.ec2.model.Filter -// snippet-end:[ec2.kotlin.running_instances.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ -suspend fun main() { - findRunningEC2Instances() -} - -// snippet-start:[ec2.kotlin.running_instances.main] -suspend fun findRunningEC2Instances() { - val filter = Filter { - name = "instance-state-name" - values = listOf("running") - } - - val request = DescribeInstancesRequest { - filters = listOf(filter) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.describeInstances(request) - response.reservations?.forEach { reservation -> - reservation.instances?.forEach { instance -> - println("Found Reservation with id: ${instance.instanceId}, type: ${instance.instanceType} state: ${instance.state?.name} and monitoring state: ${instance.monitoring?.state}") - } - } - } -} -// snippet-end:[ec2.kotlin.running_instances.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.running_instances.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.DescribeInstancesRequest +import aws.sdk.kotlin.services.ec2.model.Filter +// snippet-end:[ec2.kotlin.running_instances.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ +suspend fun main() { + findRunningEC2Instances() +} + +// snippet-start:[ec2.kotlin.running_instances.main] +suspend fun findRunningEC2Instances() { + val filter = + Filter { + name = "instance-state-name" + values = listOf("running") + } + + val request = + DescribeInstancesRequest { + filters = listOf(filter) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.describeInstances(request) + response.reservations?.forEach { reservation -> + reservation.instances?.forEach { instance -> + println( + "Found Reservation with id: ${instance.instanceId}, type: ${instance.instanceType} state: ${instance.state?.name} and monitoring state: ${instance.monitoring?.state}", + ) + } + } + } +} +// snippet-end:[ec2.kotlin.running_instances.main] diff --git a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/TerminateInstance.kt b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/TerminateInstance.kt index 1cc9a2fe402..60b63587a99 100644 --- a/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/TerminateInstance.kt +++ b/kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/TerminateInstance.kt @@ -1,51 +1,52 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -package com.kotlin.ec2 - -// snippet-start:[ec2.kotlin.terminate_instance.import] -import aws.sdk.kotlin.services.ec2.Ec2Client -import aws.sdk.kotlin.services.ec2.model.TerminateInstancesRequest -import kotlin.system.exitProcess -// snippet-end:[ec2.kotlin.terminate_instance.import] - -/** -Before running this Kotlin code example, set up your development environment, -including your credentials. - -For more information, see the following documentation topic: -https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html - */ - -suspend fun main(args: Array) { - val usage = """ - - Usage: - - - Where: - instanceID - An instance id value that you can obtain from the AWS Management Console. - """ - - if (args.size != 1) { - println(usage) - exitProcess(0) - } - - val instanceID = args[0] - terminateEC2(instanceID) -} - -// snippet-start:[ec2.kotlin.terminate_instance.main] -suspend fun terminateEC2(instanceID: String) { - val request = TerminateInstancesRequest { - instanceIds = listOf(instanceID) - } - - Ec2Client { region = "us-west-2" }.use { ec2 -> - val response = ec2.terminateInstances(request) - response.terminatingInstances?.forEach { instance -> - println("The ID of the terminated instance is ${instance.instanceId}") - } - } -} -// snippet-end:[ec2.kotlin.terminate_instance.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.kotlin.ec2 + +// snippet-start:[ec2.kotlin.terminate_instance.import] +import aws.sdk.kotlin.services.ec2.Ec2Client +import aws.sdk.kotlin.services.ec2.model.TerminateInstancesRequest +import kotlin.system.exitProcess +// snippet-end:[ec2.kotlin.terminate_instance.import] + +/** +Before running this Kotlin code example, set up your development environment, +including your credentials. + +For more information, see the following documentation topic: +https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html + */ + +suspend fun main(args: Array) { + val usage = """ + + Usage: + + + Where: + instanceID - An instance id value that you can obtain from the AWS Management Console. + """ + + if (args.size != 1) { + println(usage) + exitProcess(0) + } + + val instanceID = args[0] + terminateEC2(instanceID) +} + +// snippet-start:[ec2.kotlin.terminate_instance.main] +suspend fun terminateEC2(instanceID: String) { + val request = + TerminateInstancesRequest { + instanceIds = listOf(instanceID) + } + + Ec2Client { region = "us-west-2" }.use { ec2 -> + val response = ec2.terminateInstances(request) + response.terminatingInstances?.forEach { instance -> + println("The ID of the terminated instance is ${instance.instanceId}") + } + } +} +// snippet-end:[ec2.kotlin.terminate_instance.main] diff --git a/kotlin/services/ec2/src/test/kotlin/EC2Test.kt b/kotlin/services/ec2/src/test/kotlin/EC2Test.kt index a120921031d..89b12ce41cd 100644 --- a/kotlin/services/ec2/src/test/kotlin/EC2Test.kt +++ b/kotlin/services/ec2/src/test/kotlin/EC2Test.kt @@ -1,371 +1,392 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -import aws.sdk.kotlin.runtime.auth.credentials.EnvironmentCredentialsProvider -import aws.sdk.kotlin.services.secretsmanager.SecretsManagerClient -import aws.sdk.kotlin.services.secretsmanager.model.GetSecretValueRequest -import com.google.gson.Gson -import com.kotlin.ec2.DASHES -import com.kotlin.ec2.allocateAddressSc -import com.kotlin.ec2.associateAddressSc -import com.kotlin.ec2.createEC2Instance -import com.kotlin.ec2.createEC2KeyPair -import com.kotlin.ec2.createEC2SecurityGroup -import com.kotlin.ec2.createEC2SecurityGroupSc -import com.kotlin.ec2.createKeyPairSc -import com.kotlin.ec2.deleteEC2SecGroup -import com.kotlin.ec2.deleteEC2SecGroupSc -import com.kotlin.ec2.deleteKeys -import com.kotlin.ec2.deleteKeysSc -import com.kotlin.ec2.describeEC2Account -import com.kotlin.ec2.describeEC2Address -import com.kotlin.ec2.describeEC2Instances -import com.kotlin.ec2.describeEC2InstancesSc -import com.kotlin.ec2.describeEC2Keys -import com.kotlin.ec2.describeEC2KeysSc -import com.kotlin.ec2.describeEC2RegionsAndZones -import com.kotlin.ec2.describeEC2SecurityGroups -import com.kotlin.ec2.describeEC2Vpcs -import com.kotlin.ec2.describeImageSc -import com.kotlin.ec2.describeSecurityGroupsSc -import com.kotlin.ec2.disassociateAddressSc -import com.kotlin.ec2.findRunningEC2Instances -import com.kotlin.ec2.getInstanceTypesSc -import com.kotlin.ec2.getParaValuesSc -import com.kotlin.ec2.releaseEC2AddressSc -import com.kotlin.ec2.runInstanceSc -import com.kotlin.ec2.startInstanceSc -import com.kotlin.ec2.stopInstanceSc -import com.kotlin.ec2.terminateEC2 -import com.kotlin.ec2.terminateEC2Sc -import kotlinx.coroutines.runBlocking -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.DisplayName -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation -import org.junit.jupiter.api.Nested -import org.junit.jupiter.api.Order -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.api.TestMethodOrder -import java.io.IOException -import java.util.Random - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -@TestMethodOrder(OrderAnnotation::class) -class EC2Test { - private var instanceId = "" // Gets set in test 2. - private var ami = "" - private var instanceName = "" - private var keyName = "" - private var groupName = "" - private var groupDesc = "" - private var groupId = "" - private var vpcId = "" - - private var keyNameSc = "" - private var fileNameSc = "" - private var groupNameSc = "" - private var groupDescSc = "" - private var vpcIdSc = "" - private var myIpAddressSc = "" - - @BeforeAll - @Throws(IOException::class) - fun setUp() = runBlocking { - // Get the values to run these tests from AWS Secrets Manager. - val random = Random() - val randomNum: Int = random.nextInt(10000 - 1 + 1) + 1 - val gson = Gson() - val json: String = getSecretValues() - val values = gson.fromJson(json, SecretValues::class.java) - ami = values.ami.toString() - instanceName = values.instanceName.toString() - keyName = values.keyNameSc.toString() - groupName = values.groupName.toString() + randomNum - groupDesc = values.groupDesc.toString() - vpcId = values.vpcId.toString() - keyNameSc = values.keyNameSc.toString() + randomNum - fileNameSc = values.fileNameSc.toString() - groupDescSc = values.groupDescSc.toString() - groupNameSc = values.groupNameSc.toString() + randomNum - vpcIdSc = values.vpcIdSc.toString() - myIpAddressSc = values.myIpAddressSc.toString() - - // Uncomment this code block if you prefer using a config.properties file to retrieve AWS values required for these tests. - /* - try { - EC2Test::class.java.classLoader.getResourceAsStream("config.properties").use { input -> - val prop = Properties() - if (input == null) { - println("Sorry, unable to find config.properties") - return - } - prop.load(input) - - // Populate the data members required for all tests. - ami = prop.getProperty("ami") - instanceName = prop.getProperty("instanceName") - keyName = prop.getProperty("keyName") - groupName = prop.getProperty("groupName") - groupDesc = prop.getProperty("groupDesc") - vpcId = prop.getProperty("vpcId") - - keyNameSc = prop.getProperty("keyNameSc") - fileNameSc = prop.getProperty("fileNameSc") - groupDescSc = prop.getProperty("groupDescSc") - groupNameSc = prop.getProperty("groupNameSc") - vpcIdSc = prop.getProperty("vpcIdSc") - myIpAddressSc = prop.getProperty("myIpAddressSc") - } - } catch (ex: IOException) { - ex.printStackTrace() - } - */ - } - - @Test - @Order(1) - fun createInstanceTest() = runBlocking { - instanceId = createEC2Instance(instanceName, ami).toString() - assertTrue(instanceId.isNotEmpty()) - println("Test 1 passed") - } - - @Test - @Order(2) - fun createKeyPairTest() = runBlocking { - createEC2KeyPair(keyName) - println("Test 2 passed") - } - - @Test - @Order(3) - fun describeKeyPairTest() = runBlocking { - describeEC2Keys() - println("Test 3 passed") - } - - @Test - @Order(4) - fun deleteKeyPairTest() = runBlocking { - deleteKeys(keyName) - println("Test 4 passed") - } - - @Test - @Order(5) - fun createSecurityGroupTest() = runBlocking { - groupId = createEC2SecurityGroup(groupName, groupDesc, vpcId).toString() - assertTrue(groupId.isNotEmpty()) - println("Test 5 passed") - } - - @Test - @Order(6) - fun describeSecurityGroupTest() = runBlocking { - describeEC2SecurityGroups(groupId) - println("Test 6 passed") - } - - @Test - @Order(7) - fun deleteSecurityGroupTest() = runBlocking { - deleteEC2SecGroup(groupId) - println("Test 7 passed") - } - - @Test - @Order(8) - fun describeAccountTest() = runBlocking { - describeEC2Account() - println("Test 8 passed") - } - - @Test - @Order(9) - fun describeInstancesTest() = runBlocking { - describeEC2Instances() - println("Test 9 passed") - } - - @Test - @Order(10) - fun describeRegionsAndZonesTest() = runBlocking { - describeEC2RegionsAndZones() - println("Test 10 passed") - } - - @Test - @Order(11) - fun describeVPCsTest() = runBlocking { - describeEC2Vpcs(vpcId) - println("Test 11 passed") - } - - @Test - @Order(12) - fun findRunningInstancesTest() = runBlocking { - findRunningEC2Instances() - println("Test 12 passed") - } - - @Test - @Order(13) - fun describeAddressesTest() = runBlocking { - describeEC2Address() - println("Test 13 passed") - } - - @Test - @Order(14) - fun terminateInstanceTEst() = runBlocking { - terminateEC2(instanceId) - println("Test 14 passed") - } - - @Test - @Order(15) - fun TestEC2Scenario() = runBlocking { - var newInstanceId: String - println(DASHES) - println("1. Create an RSA key pair and save the private key material as a .pem file.") - createKeyPairSc(keyNameSc, fileNameSc) - println(DASHES) - - println(DASHES) - println("2. List key pairs.") - describeEC2KeysSc() - println(DASHES) - - println(DASHES) - println("3. Create a security group.") - val groupId = createEC2SecurityGroupSc(groupNameSc, groupDescSc, vpcIdSc, myIpAddressSc) - groupId?.let { assertTrue(it.isNotEmpty()) } - println(DASHES) - - println(DASHES) - println("4. Display security group info for the newly created security group.") - describeSecurityGroupsSc(groupId.toString()) - println(DASHES) - - println(DASHES) - println("5. Get a list of Amazon Linux 2 AMIs and select one with amzn2 in the name.") - val instanceId = getParaValuesSc() - instanceId?.let { assertTrue(it.isNotEmpty()) } - println("The instance ID is $instanceId") - println(DASHES) - - println(DASHES) - println("6. Get more information about an amzn2 image and return the AMI value.") - val amiValue = instanceId?.let { describeImageSc(it) } - amiValue?.let { assertTrue(it.isNotEmpty()) } - println("The AMI value is $amiValue.") - println(DASHES) - - println(DASHES) - println("7. Get a list of instance types.") - var instanceType = getInstanceTypesSc() - assertTrue(instanceType.isNotEmpty()) - println(DASHES) - - println(DASHES) - println("8. Create an instance.") - instanceType = "m5.large" - println("Wait 1 min before creating the instance using $instanceType") - // TimeUnit.MINUTES.sleep(1) - newInstanceId = runInstanceSc(instanceType, keyNameSc, groupNameSc, amiValue.toString()) - assertTrue(newInstanceId.isNotEmpty()) - println(DASHES) - - println(DASHES) - println("9. Display information about the running instance.") - var ipAddress = describeEC2InstancesSc(newInstanceId) - assertTrue(ipAddress.isNotEmpty()) - println("You can SSH to the instance using this command:") - println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("10. Stop the instance.") - stopInstanceSc(newInstanceId) - println(DASHES) - - println(DASHES) - println("11. Start the instance.") - startInstanceSc(newInstanceId) - ipAddress = describeEC2InstancesSc(newInstanceId) - ipAddress.let { assertTrue(it.isNotEmpty()) } - println("You can SSH to the instance using this command:") - println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("12. Allocate an Elastic IP and associate it with the instance.") - val allocationId = allocateAddressSc() - allocationId?.let { assertTrue(it.isNotEmpty()) } - val associationId = associateAddressSc(newInstanceId, allocationId) - associationId?.let { assertTrue(it.isNotEmpty()) } - println("The associate Id value is $associationId") - println(DASHES) - - println(DASHES) - println("13. Describe the instance again.") - ipAddress = describeEC2InstancesSc(newInstanceId) - ipAddress.let { assertTrue(it.isNotEmpty()) } - println("You can SSH to the instance using this command:") - println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) - println(DASHES) - - println(DASHES) - println("14. Disassociate and release the Elastic IP address.") - disassociateAddressSc(associationId) - releaseEC2AddressSc(allocationId) - println(DASHES) - - println(DASHES) - println("15. Terminate the instance and use a waiter.") - terminateEC2Sc(newInstanceId) - println(DASHES) - - println(DASHES) - println("16. Delete the security group.") - if (groupId != null) { - deleteEC2SecGroupSc(groupId) - } - println(DASHES) - - println(DASHES) - println("17. Delete the key pair.") - deleteKeysSc(keyNameSc) - println(DASHES) - } - - private suspend fun getSecretValues(): String { - val secretName = "test/ec2" - val valueRequest = GetSecretValueRequest { - secretId = secretName - } - SecretsManagerClient { region = "us-east-1"; credentialsProvider = EnvironmentCredentialsProvider() }.use { secretClient -> - val valueResponse = secretClient.getSecretValue(valueRequest) - return valueResponse.secretString.toString() - } - } - - @Nested - @DisplayName("A class used to get test values from test/ec2 (an AWS Secrets Manager secret)") - internal class SecretValues { - val ami: String? = null - val instanceName: String? = null - val keyPair: String? = null - val groupName: String? = null - val groupDesc: String? = null - val vpcId: String? = null - val keyNameSc: String? = null - val fileNameSc: String? = null - val groupNameSc: String? = null - val groupDescSc: String? = null - val vpcIdSc: String? = null - val myIpAddressSc: String? = null - } -} +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import aws.sdk.kotlin.runtime.auth.credentials.EnvironmentCredentialsProvider +import aws.sdk.kotlin.services.secretsmanager.SecretsManagerClient +import aws.sdk.kotlin.services.secretsmanager.model.GetSecretValueRequest +import com.google.gson.Gson +import com.kotlin.ec2.DASHES +import com.kotlin.ec2.allocateAddressSc +import com.kotlin.ec2.associateAddressSc +import com.kotlin.ec2.createEC2Instance +import com.kotlin.ec2.createEC2KeyPair +import com.kotlin.ec2.createEC2SecurityGroup +import com.kotlin.ec2.createEC2SecurityGroupSc +import com.kotlin.ec2.createKeyPairSc +import com.kotlin.ec2.deleteEC2SecGroup +import com.kotlin.ec2.deleteEC2SecGroupSc +import com.kotlin.ec2.deleteKeys +import com.kotlin.ec2.deleteKeysSc +import com.kotlin.ec2.describeEC2Account +import com.kotlin.ec2.describeEC2Address +import com.kotlin.ec2.describeEC2Instances +import com.kotlin.ec2.describeEC2InstancesSc +import com.kotlin.ec2.describeEC2Keys +import com.kotlin.ec2.describeEC2KeysSc +import com.kotlin.ec2.describeEC2RegionsAndZones +import com.kotlin.ec2.describeEC2SecurityGroups +import com.kotlin.ec2.describeEC2Vpcs +import com.kotlin.ec2.describeImageSc +import com.kotlin.ec2.describeSecurityGroupsSc +import com.kotlin.ec2.disassociateAddressSc +import com.kotlin.ec2.findRunningEC2Instances +import com.kotlin.ec2.getInstanceTypesSc +import com.kotlin.ec2.getParaValuesSc +import com.kotlin.ec2.releaseEC2AddressSc +import com.kotlin.ec2.runInstanceSc +import com.kotlin.ec2.startInstanceSc +import com.kotlin.ec2.stopInstanceSc +import com.kotlin.ec2.terminateEC2 +import com.kotlin.ec2.terminateEC2Sc +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Order +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.TestMethodOrder +import java.io.IOException +import java.util.Random + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestMethodOrder(OrderAnnotation::class) +class EC2Test { + private var instanceId = "" // Gets set in test 2. + private var ami = "" + private var instanceName = "" + private var keyName = "" + private var groupName = "" + private var groupDesc = "" + private var groupId = "" + private var vpcId = "" + + private var keyNameSc = "" + private var fileNameSc = "" + private var groupNameSc = "" + private var groupDescSc = "" + private var vpcIdSc = "" + private var myIpAddressSc = "" + + @BeforeAll + @Throws(IOException::class) + fun setUp() = + runBlocking { + // Get the values to run these tests from AWS Secrets Manager. + val random = Random() + val randomNum: Int = random.nextInt(10000 - 1 + 1) + 1 + val gson = Gson() + val json: String = getSecretValues() + val values = gson.fromJson(json, SecretValues::class.java) + ami = values.ami.toString() + instanceName = values.instanceName.toString() + keyName = values.keyNameSc.toString() + groupName = values.groupName.toString() + randomNum + groupDesc = values.groupDesc.toString() + vpcId = values.vpcId.toString() + keyNameSc = values.keyNameSc.toString() + randomNum + fileNameSc = values.fileNameSc.toString() + groupDescSc = values.groupDescSc.toString() + groupNameSc = values.groupNameSc.toString() + randomNum + vpcIdSc = values.vpcIdSc.toString() + myIpAddressSc = values.myIpAddressSc.toString() + + // Uncomment this code block if you prefer using a config.properties file to retrieve AWS values required for these tests. + + /* + try { + EC2Test::class.java.classLoader.getResourceAsStream("config.properties").use { input -> + val prop = Properties() + if (input == null) { + println("Sorry, unable to find config.properties") + return + } + prop.load(input) + + // Populate the data members required for all tests. + ami = prop.getProperty("ami") + instanceName = prop.getProperty("instanceName") + keyName = prop.getProperty("keyName") + groupName = prop.getProperty("groupName") + groupDesc = prop.getProperty("groupDesc") + vpcId = prop.getProperty("vpcId") + + keyNameSc = prop.getProperty("keyNameSc") + fileNameSc = prop.getProperty("fileNameSc") + groupDescSc = prop.getProperty("groupDescSc") + groupNameSc = prop.getProperty("groupNameSc") + vpcIdSc = prop.getProperty("vpcIdSc") + myIpAddressSc = prop.getProperty("myIpAddressSc") + } + } catch (ex: IOException) { + ex.printStackTrace() + } + */ + } + + @Test + @Order(1) + fun createInstanceTest() = + runBlocking { + instanceId = createEC2Instance(instanceName, ami).toString() + assertTrue(instanceId.isNotEmpty()) + println("Test 1 passed") + } + + @Test + @Order(2) + fun createKeyPairTest() = + runBlocking { + createEC2KeyPair(keyName) + println("Test 2 passed") + } + + @Test + @Order(3) + fun describeKeyPairTest() = + runBlocking { + describeEC2Keys() + println("Test 3 passed") + } + + @Test + @Order(4) + fun deleteKeyPairTest() = + runBlocking { + deleteKeys(keyName) + println("Test 4 passed") + } + + @Test + @Order(5) + fun createSecurityGroupTest() = + runBlocking { + groupId = createEC2SecurityGroup(groupName, groupDesc, vpcId).toString() + assertTrue(groupId.isNotEmpty()) + println("Test 5 passed") + } + + @Test + @Order(6) + fun describeSecurityGroupTest() = + runBlocking { + describeEC2SecurityGroups(groupId) + println("Test 6 passed") + } + + @Test + @Order(7) + fun deleteSecurityGroupTest() = + runBlocking { + deleteEC2SecGroup(groupId) + println("Test 7 passed") + } + + @Test + @Order(8) + fun describeAccountTest() = + runBlocking { + describeEC2Account() + println("Test 8 passed") + } + + @Test + @Order(9) + fun describeInstancesTest() = + runBlocking { + describeEC2Instances() + println("Test 9 passed") + } + + @Test + @Order(10) + fun describeRegionsAndZonesTest() = + runBlocking { + describeEC2RegionsAndZones() + println("Test 10 passed") + } + + @Test + @Order(11) + fun describeVPCsTest() = + runBlocking { + describeEC2Vpcs(vpcId) + println("Test 11 passed") + } + + @Test + @Order(12) + fun findRunningInstancesTest() = + runBlocking { + findRunningEC2Instances() + println("Test 12 passed") + } + + @Test + @Order(13) + fun describeAddressesTest() = + runBlocking { + describeEC2Address() + println("Test 13 passed") + } + + @Test + @Order(14) + fun terminateInstanceTest() = + runBlocking { + terminateEC2(instanceId) + println("Test 14 passed") + } + + @Test + @Order(15) + fun fullEC2ScenarioTest() = + runBlocking { + var newInstanceId: String + println(DASHES) + println("1. Create an RSA key pair and save the private key material as a .pem file.") + createKeyPairSc(keyNameSc, fileNameSc) + println(DASHES) + + println(DASHES) + println("2. List key pairs.") + describeEC2KeysSc() + println(DASHES) + + println(DASHES) + println("3. Create a security group.") + val groupId = createEC2SecurityGroupSc(groupNameSc, groupDescSc, vpcIdSc, myIpAddressSc) + groupId?.let { assertTrue(it.isNotEmpty()) } + println(DASHES) + + println(DASHES) + println("4. Display security group info for the newly created security group.") + describeSecurityGroupsSc(groupId.toString()) + println(DASHES) + + println(DASHES) + println("5. Get a list of Amazon Linux 2 AMIs and select one with amzn2 in the name.") + val instanceId = getParaValuesSc() + instanceId?.let { assertTrue(it.isNotEmpty()) } + println("The instance ID is $instanceId") + println(DASHES) + + println(DASHES) + println("6. Get more information about an amzn2 image and return the AMI value.") + val amiValue = instanceId?.let { describeImageSc(it) } + amiValue?.let { assertTrue(it.isNotEmpty()) } + println("The AMI value is $amiValue.") + println(DASHES) + + println(DASHES) + println("7. Get a list of instance types.") + var instanceType = getInstanceTypesSc() + assertTrue(instanceType.isNotEmpty()) + println(DASHES) + + println(DASHES) + println("8. Create an instance.") + instanceType = "m5.large" + println("Wait 1 min before creating the instance using $instanceType") + // TimeUnit.MINUTES.sleep(1) + newInstanceId = runInstanceSc(instanceType, keyNameSc, groupNameSc, amiValue.toString()) + assertTrue(newInstanceId.isNotEmpty()) + println(DASHES) + + println(DASHES) + println("9. Display information about the running instance.") + var ipAddress = describeEC2InstancesSc(newInstanceId) + assertTrue(ipAddress.isNotEmpty()) + println("You can SSH to the instance using this command:") + println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("10. Stop the instance.") + stopInstanceSc(newInstanceId) + println(DASHES) + + println(DASHES) + println("11. Start the instance.") + startInstanceSc(newInstanceId) + ipAddress = describeEC2InstancesSc(newInstanceId) + ipAddress.let { assertTrue(it.isNotEmpty()) } + println("You can SSH to the instance using this command:") + println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("12. Allocate an Elastic IP and associate it with the instance.") + val allocationId = allocateAddressSc() + allocationId?.let { assertTrue(it.isNotEmpty()) } + val associationId = associateAddressSc(newInstanceId, allocationId) + associationId?.let { assertTrue(it.isNotEmpty()) } + println("The associate Id value is $associationId") + println(DASHES) + + println(DASHES) + println("13. Describe the instance again.") + ipAddress = describeEC2InstancesSc(newInstanceId) + ipAddress.let { assertTrue(it.isNotEmpty()) } + println("You can SSH to the instance using this command:") + println("ssh -i " + fileNameSc + "ec2-user@" + ipAddress) + println(DASHES) + + println(DASHES) + println("14. Disassociate and release the Elastic IP address.") + disassociateAddressSc(associationId) + releaseEC2AddressSc(allocationId) + println(DASHES) + + println(DASHES) + println("15. Terminate the instance and use a waiter.") + terminateEC2Sc(newInstanceId) + println(DASHES) + + println(DASHES) + println("16. Delete the security group.") + if (groupId != null) { + deleteEC2SecGroupSc(groupId) + } + println(DASHES) + + println(DASHES) + println("17. Delete the key pair.") + deleteKeysSc(keyNameSc) + println(DASHES) + } + + private suspend fun getSecretValues(): String { + val secretName = "test/ec2" + val valueRequest = + GetSecretValueRequest { + secretId = secretName + } + SecretsManagerClient { + region = "us-east-1" + credentialsProvider = EnvironmentCredentialsProvider() + }.use { secretClient -> + val valueResponse = secretClient.getSecretValue(valueRequest) + return valueResponse.secretString.toString() + } + } + + @Nested + @DisplayName("A class used to get test values from test/ec2 (an AWS Secrets Manager secret)") + internal class SecretValues { + val ami: String? = null + val instanceName: String? = null + val keyPair: String? = null + val groupName: String? = null + val groupDesc: String? = null + val vpcId: String? = null + val keyNameSc: String? = null + val fileNameSc: String? = null + val groupNameSc: String? = null + val groupDescSc: String? = null + val vpcIdSc: String? = null + val myIpAddressSc: String? = null + } +}