My answer will give the following statement:
All I'm trying to do is create an instance and use its existing VPC group.
So, as I understand it, you want to start the instance in VPC not by default and assign it an existing VPC security group.
I am not a java guy, but I could do what you wanted in ruby , as shown below.
require 'aws-sdk-core' Aws.config = { :access_key_id => "my_access_key", :secret_access_key => "my_secret_key", :region => 'us-west-2' } ec2 = Aws::EC2.new ec2.run_instances( min_count: 1, max_count: 1, image_id: 'ami-8635a9b6', instance_type: 't1.micro', placement: { availability_zone: 'us-west-2a' }, network_interfaces: [ { subnet_id: 'subnet-e881bd63', groups: ['sg-fd53bf5e'], device_index: 0, associate_public_ip_address: true } ], key_name: 'my-key' ).each do |resp| resp.instances.each do |x| puts x.instance_id end end
Although this is ruby code, it is pretty straight forward and should give you some clear tips on how to do this in java , since all of these AWS SDKs are polling the same web services APIs.
I think you should concentrate on the code above:
:region => 'us-west-2'
and
placement: { availability_zone: 'us-west-2a' }, network_interfaces: [ { subnet_id: 'subnet-e881bd63', groups: ['sg-fd53bf5e'], device_index: 0, associate_public_ip_address: true } ],
- Make sure you explicitly specify the area.
- Check how I determined the subnet identifier and the security group identifier. This code will launch my EC2 instance in
subnet-e881bd63 my VPC and apply the VPC security group id sg-fd53bf5e to its 0 network interface. In addition, it will also assign a public IP address to my instance. (by default, it will not assign a public IP address when running instances in VPC). - FYI. When you run instances in VPC, you must specify the security group ID instead of the security group name.
source share