Skip to content

Commit 3d06746

Browse files
authored
Update tf s3 backend instructions for better ec2 credential behaviour (#871)
* update tf s3 backend instructions for better ec2 cred behaviour * avoid tokens existing for longer than requried * fix token revocation
1 parent 35bb00f commit 3d06746

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

docs/opentofu-remote-state.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ per-checkout configuration is required.
127127
openstack ec2 credentials create
128128
```
129129

130-
and make a note of the `access` field returned.
131-
132130
3. Create the backend file:
133131

134132
```shell
@@ -144,13 +142,19 @@ per-checkout configuration is required.
144142
5. Add the following to `environments/$ENV/activate`:
145143

146144
```bash
147-
export AWS_ACCESS_KEY_ID=$EC2_CREDENTIALS_ACCESS
148-
export AWS_SECRET_ACCESS_KEY=$(openstack ec2 credentials show $AWS_ACCESS_KEY_ID -f value -c secret)
145+
# Get current openstack project:
146+
TOKEN_DATA=$(openstack token issue -f json)
147+
PROJECT_ID=$(echo "$TOKEN_DATA" | jq -r '.project_id')
148+
TOKEN_ID=$(echo "$TOKEN_DATA" | jq -r '.id')
149+
openstack token revoke $TOKEN_ID
150+
151+
# Get first creds in current project:
152+
EC2_CREDS=$(openstack ec2 credentials list -f json | jq -r --arg pid "$PROJECT_ID" '.[] | select(.["Project ID"] == $pid) | @json' | head -n 1)
153+
# Set creds for OpenTofu s3 backend:
154+
export AWS_ACCESS_KEY_ID=$(echo "$EC2_CREDS" | jq -r '.Access')
155+
export AWS_SECRET_ACCESS_KEY=$(echo "$EC2_CREDS" | jq -r '.Secret')
149156
```
150157

151-
replacing `$EC2_CREDENTIALS_ACCESS` with the `access` field of the created
152-
credentials.
153-
154158
This avoids these credentials being persisted in local files.
155159

156160
6. Copy the lines above into your shell to set them for your current shell.
@@ -180,5 +184,20 @@ For more configuration options, see the OpenTofu [s3 backend docs](https://opent
180184

181185
### Per-checkout configuration
182186

183-
The ec2 credentials will automatically be loaded when activating the environment.
184-
For a new checkout simply initialise OpenTofu as normal as described in step 7 above.
187+
EC2 credentials are per-user and per-project. Check you have credentials for
188+
the current project using:
189+
190+
```shell
191+
openstack ec2 credentials list # to show credentials
192+
openstack project list # to show project IDs
193+
```
194+
195+
and if not, create them:
196+
197+
```shell
198+
openstack ec2 credentials create
199+
```
200+
201+
The ec2 credentials will then automatically be loaded when activating the
202+
environment. For a new checkout simply initialise OpenTofu as normal as
203+
described in step 7 above.

0 commit comments

Comments
 (0)