Introduction
We previously talked about OIDC integration with AWS using GitHub Actions. We will now see how to do a similar integration with BitBucket Pipelines. Namely, we want to push a container image to AWS ECR and then use that image in future pipelines.
AWS IAM Identity Providers
We have to configure the Bitbucket OpenID Connect provider in AWS IAM. Use the following to create a new OpenID Connect provider:
- Provider URL:
api.bitbucket.org/2.0/workspaces/<YOUR-WORKSPACE-NAME>/pipelines-config/identity/oidc - Audience:
ari:cloud:bitbucket::workspace/<YOUR-WORKSPACE-ID>
Now that the provider is configured, we can set up the IAM role for the Bitbucket OIDC provider and use it in our pipelines.
AWS OIDC IAM Role
The IAM role requires ECR push permissions and the GetAuthorizationToken action in order to be able to log into ECR.
Example permissions:
| |
In the trust relationships section, we need to add the arn of the previously created Bitbucket OIDC provider and allw
the AssumeRoleWithWebIdentity action for our specific Bitbucket repository. We can also add the IP addresses of the
Bitbucket servers that will be using the OIDC provider in case you are using dedicated self-hosted Bitbucket runners.
Example trust relationships:
| |
ECR Push Pipeline
After setting up the IAM role, we can use it in our pipelines as follows:
| |
The above pipeline will build the docker image and push it to ECR using the IAM role we created and using the official
Bitbucket Pipelines Pipe: AWS ECR push image atlassian/aws-ecr-push-image.
If we want to use this image as a base image in our future pipelines, we can reference it in the image name section and specify the OIDC IAM role that can be used to pull it from ECR.
| |
This way, the terraform plan step will run inside the designated container image with a pre-installed terraform binary pinned to a specific version (1.12.0-6.5.0).
Conclusion
In this post, we have seen how to use Bitbucket OIDC integration with AWS ECR. We have also seen how to use the official Bitbucket Pipelines Pipe: AWS ECR push image to build and push a docker image to ECR. Hopefully, this will help you in your future projects.
References: