

# Code examples for Amazon EC2 using AWS SDKs
<a name="service_code_examples"></a>

The following code examples show how to use Amazon EC2 with an AWS software development kit (SDK). 

*Basics* are code examples that show you how to perform the essential operations within a service.

*Actions* are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

*Scenarios* are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

For a complete list of AWS SDK developer guides and code examples, see [Create Amazon EC2 resources using an AWS SDK](sdk-general-information-section.md). This topic also includes information about getting started and details about previous SDK versions.

**Contents**
+ [Basics](service_code_examples_basics.md)
  + [Hello Amazon EC2](example_ec2_Hello_section.md)
  + [Learn the basics](example_ec2_Scenario_GetStartedInstances_section.md)
  + [Actions](service_code_examples_actions.md)
    + [`AcceptVpcPeeringConnection`](example_ec2_AcceptVpcPeeringConnection_section.md)
    + [`AllocateAddress`](example_ec2_AllocateAddress_section.md)
    + [`AllocateHosts`](example_ec2_AllocateHosts_section.md)
    + [`AssignPrivateIpAddresses`](example_ec2_AssignPrivateIpAddresses_section.md)
    + [`AssociateAddress`](example_ec2_AssociateAddress_section.md)
    + [`AssociateDhcpOptions`](example_ec2_AssociateDhcpOptions_section.md)
    + [`AssociateRouteTable`](example_ec2_AssociateRouteTable_section.md)
    + [`AttachInternetGateway`](example_ec2_AttachInternetGateway_section.md)
    + [`AttachNetworkInterface`](example_ec2_AttachNetworkInterface_section.md)
    + [`AttachVolume`](example_ec2_AttachVolume_section.md)
    + [`AttachVpnGateway`](example_ec2_AttachVpnGateway_section.md)
    + [`AuthorizeSecurityGroupEgress`](example_ec2_AuthorizeSecurityGroupEgress_section.md)
    + [`AuthorizeSecurityGroupIngress`](example_ec2_AuthorizeSecurityGroupIngress_section.md)
    + [`CancelCapacityReservation`](example_ec2_CancelCapacityReservation_section.md)
    + [`CancelImportTask`](example_ec2_CancelImportTask_section.md)
    + [`CancelSpotFleetRequests`](example_ec2_CancelSpotFleetRequests_section.md)
    + [`CancelSpotInstanceRequests`](example_ec2_CancelSpotInstanceRequests_section.md)
    + [`ConfirmProductInstance`](example_ec2_ConfirmProductInstance_section.md)
    + [`CopyImage`](example_ec2_CopyImage_section.md)
    + [`CopySnapshot`](example_ec2_CopySnapshot_section.md)
    + [`CreateCapacityReservation`](example_ec2_CreateCapacityReservation_section.md)
    + [`CreateCustomerGateway`](example_ec2_CreateCustomerGateway_section.md)
    + [`CreateDhcpOptions`](example_ec2_CreateDhcpOptions_section.md)
    + [`CreateFlowLogs`](example_ec2_CreateFlowLogs_section.md)
    + [`CreateImage`](example_ec2_CreateImage_section.md)
    + [`CreateInstanceExportTask`](example_ec2_CreateInstanceExportTask_section.md)
    + [`CreateInternetGateway`](example_ec2_CreateInternetGateway_section.md)
    + [`CreateKeyPair`](example_ec2_CreateKeyPair_section.md)
    + [`CreateLaunchTemplate`](example_ec2_CreateLaunchTemplate_section.md)
    + [`CreateNetworkAcl`](example_ec2_CreateNetworkAcl_section.md)
    + [`CreateNetworkAclEntry`](example_ec2_CreateNetworkAclEntry_section.md)
    + [`CreateNetworkInterface`](example_ec2_CreateNetworkInterface_section.md)
    + [`CreatePlacementGroup`](example_ec2_CreatePlacementGroup_section.md)
    + [`CreateRoute`](example_ec2_CreateRoute_section.md)
    + [`CreateRouteTable`](example_ec2_CreateRouteTable_section.md)
    + [`CreateSecurityGroup`](example_ec2_CreateSecurityGroup_section.md)
    + [`CreateSnapshot`](example_ec2_CreateSnapshot_section.md)
    + [`CreateSpotDatafeedSubscription`](example_ec2_CreateSpotDatafeedSubscription_section.md)
    + [`CreateSubnet`](example_ec2_CreateSubnet_section.md)
    + [`CreateTags`](example_ec2_CreateTags_section.md)
    + [`CreateVolume`](example_ec2_CreateVolume_section.md)
    + [`CreateVpc`](example_ec2_CreateVpc_section.md)
    + [`CreateVpcEndpoint`](example_ec2_CreateVpcEndpoint_section.md)
    + [`CreateVpnConnection`](example_ec2_CreateVpnConnection_section.md)
    + [`CreateVpnConnectionRoute`](example_ec2_CreateVpnConnectionRoute_section.md)
    + [`CreateVpnGateway`](example_ec2_CreateVpnGateway_section.md)
    + [`DeleteCustomerGateway`](example_ec2_DeleteCustomerGateway_section.md)
    + [`DeleteDhcpOptions`](example_ec2_DeleteDhcpOptions_section.md)
    + [`DeleteFlowLogs`](example_ec2_DeleteFlowLogs_section.md)
    + [`DeleteInternetGateway`](example_ec2_DeleteInternetGateway_section.md)
    + [`DeleteKeyPair`](example_ec2_DeleteKeyPair_section.md)
    + [`DeleteLaunchTemplate`](example_ec2_DeleteLaunchTemplate_section.md)
    + [`DeleteNetworkAcl`](example_ec2_DeleteNetworkAcl_section.md)
    + [`DeleteNetworkAclEntry`](example_ec2_DeleteNetworkAclEntry_section.md)
    + [`DeleteNetworkInterface`](example_ec2_DeleteNetworkInterface_section.md)
    + [`DeletePlacementGroup`](example_ec2_DeletePlacementGroup_section.md)
    + [`DeleteRoute`](example_ec2_DeleteRoute_section.md)
    + [`DeleteRouteTable`](example_ec2_DeleteRouteTable_section.md)
    + [`DeleteSecurityGroup`](example_ec2_DeleteSecurityGroup_section.md)
    + [`DeleteSnapshot`](example_ec2_DeleteSnapshot_section.md)
    + [`DeleteSpotDatafeedSubscription`](example_ec2_DeleteSpotDatafeedSubscription_section.md)
    + [`DeleteSubnet`](example_ec2_DeleteSubnet_section.md)
    + [`DeleteTags`](example_ec2_DeleteTags_section.md)
    + [`DeleteVolume`](example_ec2_DeleteVolume_section.md)
    + [`DeleteVpc`](example_ec2_DeleteVpc_section.md)
    + [`DeleteVpcEndpoints`](example_ec2_DeleteVpcEndpoints_section.md)
    + [`DeleteVpnConnection`](example_ec2_DeleteVpnConnection_section.md)
    + [`DeleteVpnConnectionRoute`](example_ec2_DeleteVpnConnectionRoute_section.md)
    + [`DeleteVpnGateway`](example_ec2_DeleteVpnGateway_section.md)
    + [`DeregisterImage`](example_ec2_DeregisterImage_section.md)
    + [`DescribeAccountAttributes`](example_ec2_DescribeAccountAttributes_section.md)
    + [`DescribeAddresses`](example_ec2_DescribeAddresses_section.md)
    + [`DescribeAvailabilityZones`](example_ec2_DescribeAvailabilityZones_section.md)
    + [`DescribeBundleTasks`](example_ec2_DescribeBundleTasks_section.md)
    + [`DescribeCapacityReservations`](example_ec2_DescribeCapacityReservations_section.md)
    + [`DescribeCustomerGateways`](example_ec2_DescribeCustomerGateways_section.md)
    + [`DescribeDhcpOptions`](example_ec2_DescribeDhcpOptions_section.md)
    + [`DescribeFlowLogs`](example_ec2_DescribeFlowLogs_section.md)
    + [`DescribeHostReservationOfferings`](example_ec2_DescribeHostReservationOfferings_section.md)
    + [`DescribeHosts`](example_ec2_DescribeHosts_section.md)
    + [`DescribeIamInstanceProfileAssociations`](example_ec2_DescribeIamInstanceProfileAssociations_section.md)
    + [`DescribeIdFormat`](example_ec2_DescribeIdFormat_section.md)
    + [`DescribeIdentityIdFormat`](example_ec2_DescribeIdentityIdFormat_section.md)
    + [`DescribeImageAttribute`](example_ec2_DescribeImageAttribute_section.md)
    + [`DescribeImages`](example_ec2_DescribeImages_section.md)
    + [`DescribeImportImageTasks`](example_ec2_DescribeImportImageTasks_section.md)
    + [`DescribeImportSnapshotTasks`](example_ec2_DescribeImportSnapshotTasks_section.md)
    + [`DescribeInstanceAttribute`](example_ec2_DescribeInstanceAttribute_section.md)
    + [`DescribeInstanceStatus`](example_ec2_DescribeInstanceStatus_section.md)
    + [`DescribeInstanceTypes`](example_ec2_DescribeInstanceTypes_section.md)
    + [`DescribeInstances`](example_ec2_DescribeInstances_section.md)
    + [`DescribeInternetGateways`](example_ec2_DescribeInternetGateways_section.md)
    + [`DescribeKeyPairs`](example_ec2_DescribeKeyPairs_section.md)
    + [`DescribeNetworkAcls`](example_ec2_DescribeNetworkAcls_section.md)
    + [`DescribeNetworkInterfaceAttribute`](example_ec2_DescribeNetworkInterfaceAttribute_section.md)
    + [`DescribeNetworkInterfaces`](example_ec2_DescribeNetworkInterfaces_section.md)
    + [`DescribePlacementGroups`](example_ec2_DescribePlacementGroups_section.md)
    + [`DescribePrefixLists`](example_ec2_DescribePrefixLists_section.md)
    + [`DescribeRegions`](example_ec2_DescribeRegions_section.md)
    + [`DescribeRouteTables`](example_ec2_DescribeRouteTables_section.md)
    + [`DescribeScheduledInstanceAvailability`](example_ec2_DescribeScheduledInstanceAvailability_section.md)
    + [`DescribeScheduledInstances`](example_ec2_DescribeScheduledInstances_section.md)
    + [`DescribeSecurityGroups`](example_ec2_DescribeSecurityGroups_section.md)
    + [`DescribeSnapshotAttribute`](example_ec2_DescribeSnapshotAttribute_section.md)
    + [`DescribeSnapshots`](example_ec2_DescribeSnapshots_section.md)
    + [`DescribeSpotDatafeedSubscription`](example_ec2_DescribeSpotDatafeedSubscription_section.md)
    + [`DescribeSpotFleetInstances`](example_ec2_DescribeSpotFleetInstances_section.md)
    + [`DescribeSpotFleetRequestHistory`](example_ec2_DescribeSpotFleetRequestHistory_section.md)
    + [`DescribeSpotFleetRequests`](example_ec2_DescribeSpotFleetRequests_section.md)
    + [`DescribeSpotInstanceRequests`](example_ec2_DescribeSpotInstanceRequests_section.md)
    + [`DescribeSpotPriceHistory`](example_ec2_DescribeSpotPriceHistory_section.md)
    + [`DescribeSubnets`](example_ec2_DescribeSubnets_section.md)
    + [`DescribeTags`](example_ec2_DescribeTags_section.md)
    + [`DescribeVolumeAttribute`](example_ec2_DescribeVolumeAttribute_section.md)
    + [`DescribeVolumeStatus`](example_ec2_DescribeVolumeStatus_section.md)
    + [`DescribeVolumes`](example_ec2_DescribeVolumes_section.md)
    + [`DescribeVpcAttribute`](example_ec2_DescribeVpcAttribute_section.md)
    + [`DescribeVpcClassicLink`](example_ec2_DescribeVpcClassicLink_section.md)
    + [`DescribeVpcClassicLinkDnsSupport`](example_ec2_DescribeVpcClassicLinkDnsSupport_section.md)
    + [`DescribeVpcEndpointServices`](example_ec2_DescribeVpcEndpointServices_section.md)
    + [`DescribeVpcEndpoints`](example_ec2_DescribeVpcEndpoints_section.md)
    + [`DescribeVpcs`](example_ec2_DescribeVpcs_section.md)
    + [`DescribeVpnConnections`](example_ec2_DescribeVpnConnections_section.md)
    + [`DescribeVpnGateways`](example_ec2_DescribeVpnGateways_section.md)
    + [`DetachInternetGateway`](example_ec2_DetachInternetGateway_section.md)
    + [`DetachNetworkInterface`](example_ec2_DetachNetworkInterface_section.md)
    + [`DetachVolume`](example_ec2_DetachVolume_section.md)
    + [`DetachVpnGateway`](example_ec2_DetachVpnGateway_section.md)
    + [`DisableVgwRoutePropagation`](example_ec2_DisableVgwRoutePropagation_section.md)
    + [`DisableVpcClassicLink`](example_ec2_DisableVpcClassicLink_section.md)
    + [`DisableVpcClassicLinkDnsSupport`](example_ec2_DisableVpcClassicLinkDnsSupport_section.md)
    + [`DisassociateAddress`](example_ec2_DisassociateAddress_section.md)
    + [`DisassociateRouteTable`](example_ec2_DisassociateRouteTable_section.md)
    + [`EnableVgwRoutePropagation`](example_ec2_EnableVgwRoutePropagation_section.md)
    + [`EnableVolumeIo`](example_ec2_EnableVolumeIo_section.md)
    + [`EnableVpcClassicLink`](example_ec2_EnableVpcClassicLink_section.md)
    + [`EnableVpcClassicLinkDnsSupport`](example_ec2_EnableVpcClassicLinkDnsSupport_section.md)
    + [`GetConsoleOutput`](example_ec2_GetConsoleOutput_section.md)
    + [`GetHostReservationPurchasePreview`](example_ec2_GetHostReservationPurchasePreview_section.md)
    + [`GetPasswordData`](example_ec2_GetPasswordData_section.md)
    + [`ImportImage`](example_ec2_ImportImage_section.md)
    + [`ImportKeyPair`](example_ec2_ImportKeyPair_section.md)
    + [`ImportSnapshot`](example_ec2_ImportSnapshot_section.md)
    + [`ModifyCapacityReservation`](example_ec2_ModifyCapacityReservation_section.md)
    + [`ModifyHosts`](example_ec2_ModifyHosts_section.md)
    + [`ModifyIdFormat`](example_ec2_ModifyIdFormat_section.md)
    + [`ModifyImageAttribute`](example_ec2_ModifyImageAttribute_section.md)
    + [`ModifyInstanceAttribute`](example_ec2_ModifyInstanceAttribute_section.md)
    + [`ModifyInstanceCreditSpecification`](example_ec2_ModifyInstanceCreditSpecification_section.md)
    + [`ModifyNetworkInterfaceAttribute`](example_ec2_ModifyNetworkInterfaceAttribute_section.md)
    + [`ModifyReservedInstances`](example_ec2_ModifyReservedInstances_section.md)
    + [`ModifySnapshotAttribute`](example_ec2_ModifySnapshotAttribute_section.md)
    + [`ModifySpotFleetRequest`](example_ec2_ModifySpotFleetRequest_section.md)
    + [`ModifySubnetAttribute`](example_ec2_ModifySubnetAttribute_section.md)
    + [`ModifyVolumeAttribute`](example_ec2_ModifyVolumeAttribute_section.md)
    + [`ModifyVpcAttribute`](example_ec2_ModifyVpcAttribute_section.md)
    + [`MonitorInstances`](example_ec2_MonitorInstances_section.md)
    + [`MoveAddressToVpc`](example_ec2_MoveAddressToVpc_section.md)
    + [`PurchaseHostReservation`](example_ec2_PurchaseHostReservation_section.md)
    + [`PurchaseScheduledInstances`](example_ec2_PurchaseScheduledInstances_section.md)
    + [`RebootInstances`](example_ec2_RebootInstances_section.md)
    + [`RegisterImage`](example_ec2_RegisterImage_section.md)
    + [`RejectVpcPeeringConnection`](example_ec2_RejectVpcPeeringConnection_section.md)
    + [`ReleaseAddress`](example_ec2_ReleaseAddress_section.md)
    + [`ReleaseHosts`](example_ec2_ReleaseHosts_section.md)
    + [`ReplaceIamInstanceProfileAssociation`](example_ec2_ReplaceIamInstanceProfileAssociation_section.md)
    + [`ReplaceNetworkAclAssociation`](example_ec2_ReplaceNetworkAclAssociation_section.md)
    + [`ReplaceNetworkAclEntry`](example_ec2_ReplaceNetworkAclEntry_section.md)
    + [`ReplaceRoute`](example_ec2_ReplaceRoute_section.md)
    + [`ReplaceRouteTableAssociation`](example_ec2_ReplaceRouteTableAssociation_section.md)
    + [`ReportInstanceStatus`](example_ec2_ReportInstanceStatus_section.md)
    + [`RequestSpotFleet`](example_ec2_RequestSpotFleet_section.md)
    + [`RequestSpotInstances`](example_ec2_RequestSpotInstances_section.md)
    + [`ResetImageAttribute`](example_ec2_ResetImageAttribute_section.md)
    + [`ResetInstanceAttribute`](example_ec2_ResetInstanceAttribute_section.md)
    + [`ResetNetworkInterfaceAttribute`](example_ec2_ResetNetworkInterfaceAttribute_section.md)
    + [`ResetSnapshotAttribute`](example_ec2_ResetSnapshotAttribute_section.md)
    + [`RevokeSecurityGroupEgress`](example_ec2_RevokeSecurityGroupEgress_section.md)
    + [`RevokeSecurityGroupIngress`](example_ec2_RevokeSecurityGroupIngress_section.md)
    + [`RunInstances`](example_ec2_RunInstances_section.md)
    + [`RunScheduledInstances`](example_ec2_RunScheduledInstances_section.md)
    + [`StartInstances`](example_ec2_StartInstances_section.md)
    + [`StopInstances`](example_ec2_StopInstances_section.md)
    + [`TerminateInstances`](example_ec2_TerminateInstances_section.md)
    + [`UnassignPrivateIpAddresses`](example_ec2_UnassignPrivateIpAddresses_section.md)
    + [`UnmonitorInstances`](example_ec2_UnmonitorInstances_section.md)
    + [`UpdateSecurityGroupRuleDescriptionsIngress`](example_ec2_UpdateSecurityGroupRuleDescriptionsIngress_section.md)
+ [Scenarios](service_code_examples_scenarios.md)
  + [Build and manage a resilient service](example_cross_ResilientService_section.md)
  + [Configure Amazon ECS Service Connect](example_ecs_ServiceConnect_085_section.md)
  + [Create a basic VPC](example_vpc_GettingStartedCLI_section.md)
  + [Create an Amazon ECS Linux task for the Fargate launch type](example_ecs_GettingStarted_086_section.md)
  + [Creating an Amazon ECS service for the EC2 launch type](example_ecs_GettingStarted_018_section.md)
  + [Creating an Amazon RDS DB instance](example_rds_GettingStarted_036_section.md)
  + [Creating and managing Amazon EBS volumes](example_ec2_GettingStarted_020_section.md)
  + [Creating and managing a VPC Lattice service network](example_vpc_lattice_GettingStarted_055_section.md)
  + [Get started with Aws Direct Connect](example_directconnect_GettingStarted_051_section.md)
  + [Get started with Marketplace Buyer](example_ec2_GettingStarted_030_section.md)
  + [Getting started with Amazon DocumentDB](example_docdb_GettingStarted_025_section.md)
  + [Getting started with Amazon EC2](example_ec2_GettingStarted_013_section.md)
  + [Getting started with Amazon EMR](example_emr_GettingStarted_037_section.md)
  + [Getting started with Amazon ElastiCache](example_ec2_GettingStarted_065_section.md)
  + [Getting started with Amazon MSK](example_ec2_GettingStarted_057_section.md)
  + [Getting started with Amazon Neptune](example_ec2_GettingStarted_064_section.md)
  + [Getting started with Amazon VPC Transit Gateway](example_vpc_TransitGatewayGettingStarted_section.md)
  + [Getting started with Elastic Load Balancing](example_elastic_load_balancing_v2_GettingStarted_058_section.md)
  + [Getting started with Elemental MediaConnect](example_mediaconnect_GettingStarted_081_section.md)
  + [Run CPU stress tests on EC2 instances using FIS](example_iam_GettingStarted_069_section.md)
  + [VPC with IPAM](example_vpc_GettingStartedIpam_section.md)
  + [VPC with private servers](example_vpc_GettingStartedPrivate_section.md)
  + [Working with Amazon EBS encryption, snapshots, and volume initialization](example_ec2_GettingStarted_022_section.md)
  + [Working with VPC peering connections](example_ec2_GettingStarted_015_section.md)