Skip to content

Commit

Permalink
add cdk infra setup for ecr images (#10678)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzon committed Apr 17, 2024
1 parent 01b9d17 commit 2dd0475
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions localstack/testing/scenario/cdk_lambda_helper.py
@@ -1,3 +1,4 @@
import base64
import os
import shutil
import tempfile
Expand All @@ -8,9 +9,11 @@
from botocore.exceptions import ClientError

from localstack.utils.aws.resources import create_s3_bucket
from localstack.utils.docker_utils import DOCKER_CLIENT
from localstack.utils.run import LOG, run

if TYPE_CHECKING:
from mypy_boto3_ecr import ECRClient
from mypy_boto3_s3 import S3Client


Expand Down Expand Up @@ -144,6 +147,34 @@ def _zip_lambda_resources(
temp_zip.write(file_path, archive_name)


def generate_ecr_image_from_dockerfile(
ecr_client: "ECRClient", repository_name: str, file_path: str
):
"""
Helper function to generate an ECR image from a dockerfile.
:param ecr_client: client for ECR
:param repository_name: name for the repository to be created
:param file_path: path of the file to be used
:return: None
"""
repository_uri = ecr_client.create_repository(
repositoryName=repository_name,
)["repository"]["repositoryUri"]

auth_response = ecr_client.get_authorization_token()
auth_token = auth_response["authorizationData"][0]["authorizationToken"].encode()
username, password = base64.b64decode(auth_token).decode().split(":")
registry = auth_response["authorizationData"][0]["proxyEndpoint"]
DOCKER_CLIENT.login(username, password, registry=registry)

temp_dir = tempfile.mkdtemp()
destination_file = os.path.join(temp_dir, "Dockerfile")
shutil.copy2(file_path, destination_file)
DOCKER_CLIENT.build_image(dockerfile_path=destination_file, image_name=repository_uri)
DOCKER_CLIENT.push_image(repository_uri)


def _upload_to_s3(s3_client: "S3Client", bucket_name: str, key_name: str, file: str):
try:
create_s3_bucket(bucket_name, s3_client)
Expand Down

0 comments on commit 2dd0475

Please sign in to comment.