-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,800 additions
and
1,692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 62 additions & 60 deletions
122
kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/AllocateAddress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String>) { | ||
val usage = """ | ||
Usage: | ||
<instanceId> | ||
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<String>) { | ||
val usage = """ | ||
Usage: | ||
<instanceId> | ||
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] |
144 changes: 75 additions & 69 deletions
144
kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateInstance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String>) { | ||
val usage = """ | ||
Usage: | ||
<name> <amiId> | ||
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<String>) { | ||
val usage = """ | ||
Usage: | ||
<name> <amiId> | ||
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] |
99 changes: 50 additions & 49 deletions
99
kotlin/services/ec2/src/main/kotlin/com/kotlin/ec2/CreateKeyPair.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String>) { | ||
val usage = """ | ||
Usage: | ||
<keyName> | ||
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<String>) { | ||
val usage = """ | ||
Usage: | ||
<keyName> | ||
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] |
Oops, something went wrong.