Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2 Java SDK - User data script

I'm looking for a way to attach a user data script to an EC2 RunRequest in the Java SDK (the equivalent of ec2-run-instances ami-1234567 -f startup-script.zip for the command line tool).

Several things I've read indicate that anything user data string with "#! " will execute, but this doesn't seem to be the case.

Is this even possible?

FYI: here's my test class:

public class AWSTest {

    public static void main(String[] args) {

        AWSCredentials credentials = new BasicAWSCredentials("access-key","secret-access-key");
        AmazonEC2Client ec2 = new AmazonEC2Client(credentials);
        RunInstancesRequest request = new RunInstancesRequest();
        request.setInstanceType(InstanceType.M1Small.toString());
        request.setMinCount(1);
        request.setMaxCount(1);
        request.setImageId("ami-84db39ed");
        request.setKeyName("linux-keypair");
        request.setUserData(getUserDataScript());
        ec2.runInstances(request);    
    }

    private static String getUserDataScript(){
        ArrayList<String> lines = new ArrayList<String>();
        lines.add("#! /bin/bash");
        lines.add("curl http://www.google.com > google.html");
        lines.add("shutdown -h 0");
        String str = new String(Base64.encodeBase64(join(lines, "\n").getBytes()));
        return str;
    }

    static String join(Collection<String> s, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<String> iter = s.iterator();
        while (iter.hasNext()) {
            builder.append(iter.next());
            if (!iter.hasNext()) {
                break;
            }
            builder.append(delimiter);
        }
        return builder.toString();
    }

}

Unfortunately, after I run this, I'm able to SSH into the box, and confirm that

  • It hasn't shut down and
  • It didn't download the file

Any assistance is greatly appreciated.

Best,

Zach

like image 585
Zachary Ozer Avatar asked Jul 01 '10 22:07

Zachary Ozer


People also ask

Where is the user data script in EC2 instance runs?

When a user data script is processed, it is copied to and run from /var/lib/cloud/instances/ instance-id / . The script is not deleted after it is run. Be sure to delete the user data scripts from /var/lib/cloud/instances/ instance-id / before you create an AMI from the instance.

What is a Userdata script?

What is UserData? EC2 user data is a set of instructions used to configure an EC2 instance at launch time using cloud-init or shell scripting. Like the EC2 meta-data service, the user data is accessible from within the instance itself using the link-local IP address 169.254. 169.254 (also known as Metadata service).

How do I access metadata in EC2?

To view instance metadata, you can only use the link-local address of 169.254. 169.254 to access. Requests to the metadata via the URI are free, so there are no additional charges from AWS. Using the curl tool on Linux or the PowerShell cmdlet Invoke-WebRequest on Windows, you will first create your token.


1 Answers

This works to insert user data in an instance run request, in this case specifically to join an ECS cluster:

private static String getECSuserData(String clusterName) {
    String userData = "";
    userData = userData + "#!/bin/bash" + "\n";
    userData = userData + "echo ECS_CLUSTER=" + clusterName + " ";
    userData = userData + ">> /etc/ecs/ecs.config";
    String base64UserData = null;
    try {
        base64UserData = new String( Base64.encodeBase64( userData.getBytes( "UTF-8" )), "UTF-8" );
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return base64UserData;
}
like image 54
Pablo Rodriguez Bertorello Avatar answered Sep 18 '22 10:09

Pablo Rodriguez Bertorello