
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 )
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"
Then use pipenv to create a requirements file:
pipenv lock -r > src/requirements.txt
Then use the requirements.txt to pull the dependancies:
sam build CommonLayer --template template.yaml --use-container --base-dir .
The result is the ability to pull your private library. Simple as that!
Top comments(1)

- LocationLYON
- WorkCloud Architect at GFI
- Joined
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,
For further actions, you may consider blocking this person and/orreporting abuse