DEV Community

Cover image for Using CodeArtifact from SAM
philbasford
philbasford

Posted on

Using CodeArtifact from SAM

Following my blog (https://www.inawisdom.com/amazon/codeartifact-storing-your-dependencies/) I have worked out how you can use private dependancies in CodeArtifact with SAM. The following is a quick guide.

Firstly use the get-authorization-token to get a secure token for CodeArtifact using your AWS CLI and store it in a env var called CODEARTIFACT_AUTH_TOKEN:

export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain my-domain --domain-owner 112333322 --query authorizationToken --output text --profile my-profile --region eu-west-1 )
Enter fullscreen mode Exit fullscreen mode

Then create a Pipfile with the CodeArtifact URL but note the use of the CODEARTIFACT_AUTH_TOKEN env var:

[[source]]
name = "pypi"
url = "https://aws:$CODEARTIFACT_AUTH_TOKEN@my-domain-12333222.d.codeartifact.eu-west-1.amazonaws.com/pypi/PrivatePyPi/simple/"
verify_ssl = true

[dev-packages]
pylint = "*"
#awscli = "==1.16.292"
#aws-sam-cli = "==0.40.0"
flake8 = "*"
flake8-print = "==3.1.4"
flake8-logging-format = "==0.6.0"
flake8-builtins = "==1.4.2"
flake8-eradicate = "==0.3.0"
flake8-comprehensions = "==3.2.2"
flake8-breakpoint = "==1.1.0"
flake8-docstrings = "*"
flake8-rst-docstrings = "*"
flake8-blind-except = "*"
pep8-naming = "==0.9.1"
cfn-lint = "==0.27.4"
pytest = "*"
pytest-cov = "==2.8.1"
moto = "*"
bandit = "*"
safety = "*"
twine = "*"

[packages]
pcb_common = "*"

[requires]
python_version = "3.8"
Enter fullscreen mode Exit fullscreen mode

Then use pipenv to create a requirements file:

pipenv lock -r > src/requirements.txt
Enter fullscreen mode Exit fullscreen mode

Then use the requirements.txt to pull the dependancies:

sam build  CommonLayer --template template.yaml --use-container --base-dir .
Enter fullscreen mode Exit fullscreen mode

The result is the ability to pull your private library. Simple as that!

Discussion (1)

Collapse
blueskycorner profile image
blueskycorner

Hi,
Impossible to use sam build with a container. It is not able to fetch my private package inside codeartifact.
Is it possible to show the content of your requirements.txt?
Best regards,