doctl
doctl copied to clipboard
create droplet in a specific project
digitalocean can group droplets into "projects".
"doctl compute droplet create" will create droplets into the default project.
how can I create a droplet in a specific project?
Hey @viorel-anghel,
To create a Droplet in a specific project, you'll need to use an API token for that project. There are two ways to do this.
Through a CLI argument
doctl allows you to pass a -t
flag to provide an API access token for that command. For example you could create a Droplet by running
doctl compute droplet create <name> -t <project-api-token> --image ubuntu-18-04-x64 --region nyc3 --size s-1vcpu-1gb
where,
-
<name>
is the name of the Droplet you want to create (as you probably already know), -
<project-api-token>
is the API token belonging to the specific project you want to create the Droplet in.
Using an authentication context
doctl allows you to create named authentication contexts so that you don't need to pass a -t
flag every time. To do this,
- Run
doctl auth init --context=<project-name>
, where<project-name>
is the name of your project. - Provide your project's API token at the prompt.
- Switch to that context by running
doctl auth switch --context=<project-name>
.
Now, anytime you create a Droplet, it will be created using your project's API token until you switch back to your default context. To do that, run doctl auth switch --context=default
. You can also view the different contexts you've created at any time by running doctl auth list
.
Hope this helps!
I have a question.
How to create the <project-api-token>
?
I have only a personal API token.
Same question here.
Hi all,
It looks like there was a bit of miscommunication around the different features and terminology. The approach @bentranter describes above is for managing multiple DigitalOcean team or accounts. The DigitalOcean API does not currently support assigning resources to a specific project when the resource is created. Currently, this require a second API call. Using doctl, that command would look like:
doctl projects resources assign <project-id> --resource=<urn> [--resource=<urn> ...]
The --resource
flag can be repeated multiple times to assign more than one resource in a single request. It takes a resource's uniform resource name (URN). For a Droplet, that looks like do:droplet:123456
See: https://developers.digitalocean.com/documentation/v2/#project-resources
Putting it all together, here's an example of what this might look like in a script:
PROJECT_ID="c82f8368-8dc5-45fa-8ffe-28038935f5ce"
DROPLET_ID=$(doctl compute droplet create example.com --size s-1vcpu-1gb --image ubuntu-20-04-x64 --region nyc3 --wait --no-header --format=ID)
doctl projects resources assign ${PROJECT_ID} --resource=do:droplet:${DROPLET_ID}
In the Droplet create request, passing --no-header --format=ID
means that only the ID is printed in response. This allows you to save it into a variable for later use.
Thanks for the tips!
Thanks @andrewsomething! It would be nice to have a version of your explanation in the official documentation as well, or to be able to use
doctl compute droplet create example.com \
--size s-1vcpu-1gb \
--image ubuntu-20-04-x64 \
--region nyc3 \
--wait \
--format ID,Name,Tags,PublicIPv4,PrivateIPv4,Status \
--context $CONTEXT \
--project $PROJECT_NAME
For example.
Hi guys, is it means doctl not support to get project ID yet?
@andrewsomething do you know if the --project flag for creating resources is on the development schedule?
@andrewsomething Can I take this task of allowing the project id to be passed in create droplet command. I would like to contribute.
Hello @BBudnicki ! Adding resources by project is one of the very important features we at DO are working, albeit it is only in the planning stages as of now. We look forward to rolling this feature out in the not too distant future. Thank you.
bump
bump Being able to specify a project ID/name is optimal when creating resources in DO.
Bump,
Asking for project-id to be specified during droplet creation
3 years and a feature that probably shouldn't be that hard to implement?
Hey @brandonscholet and @shkarface, this is available in our latest release – you can provide a project UUID when creating a Droplet with a new --project-id
flag.
@bentranter thank you for letting us know! Appreciate it.