1

I'm working with VSCode dev containers, and the container should pip install modules that reside on AWS CodeArtifact. First, I need to sso login on the host machine, before the container is built, so I added the following configuration to devcontainer.json:

{
  ...
  "initializeCommand": "aws sso login --profile dev"
  ...
}

Here lies my problem. The container should install the modules, so I need it to have the proper permissions. I run $ aws codeartifact get-authorization-token on the host machine and I need to pass the token to the container. How can I do it? As far as I know there is no option to pass dynamic values as an environment variable, and using script to create files seems very cumbersome for such a simple task.

1 Answer 1

2

I've searched this for a while then found such solution: The idea is initializeCommand runs on the host machine and generates a file named token in project directory. Then postCreateCommand can use this file to login to codeartifact repo. Hope this helps.

cat devcontainer.json

{
    "name": "C# (.NET)",
    "image": "mcr.microsoft.com/devcontainers/dotnet:1-8.0-bookworm",
    "features": {
        "ghcr.io/devcontainers/features/aws-cli:1": {
            "version": "latest"
        },
        "ghcr.io/devcontainers/features/github-cli:1": {
            "installDirectlyFromGitHubRelease": true,
            "version": "latest"
        }
    },
    "forwardPorts": [5002],
    "portsAttributes": {
        "5001": {
            "protocol": "https"
        }
},  
"initializeCommand": "aws codeartifact get-authorization-token --domain xxxx --domain-owner yyyy --region zzzz --query authorizationToken --output text > token",
"postCreateCommand": "dotnet nuget add source \"https://xxxx-yyyy.d.codeartifact.zzzz.amazonaws.com/nuget/nuget/v3/index.json\" -n \"somename\" -u \"aws\" -p \"$(cat token)\" --store-password-in-clear-text && dotnet restore && rm -f token"
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great idea! What I finally did was to create the image using a script, and the devcontainer.json file just pointed to that image, without building it.

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.