1

How do I set ECS_CLUSTER=my_cluster_name in user-data when launching a new EC2 instance using the AWS .NET SDK?

I've found info on doing this manually by running a bash script on the machine. But I'm specifically interested in learning how to do this programmatically, using the SDK.

1 Answer 1

1

Found it in the RunInstancesRequest class. The key for me was not only in finding the UserData field, but also in including the IamInstanceProfile. Here's an example:

string userDataString = $"#!/bin/bash \necho ECS_CLUSTER=my_cluster_name >> /etc/ecs/ecs.config";
string userData = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(userDataString));
var response = client.RunInstances(new RunInstancesRequest 
{
    ImageId = "ami-abc12345",
    InstanceType = "t2.micro",
    KeyName = "my-key-pair",
    MaxCount = 1,
    MinCount = 1,
    SecurityGroupIds = new List<string> { "sg-1a2b3c4d"},
    SubnetId = "subnet-6e7f829e",
    UserData = userData,
    IamInstanceProfile = new IamInstanceProfileSpecification { Name = "ecsInstanceRole" }
});

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TRunInstancesRequest.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.