Пример #1
0
 def _add_init_script(self) -> None:
     """Copy the init script defined by the user to the payload folder."""
     if self.resources_info.get('lambda').get('init_script', False):
         init_script_path = self.resources_info.get('lambda').get('init_script')
         FileUtils.copy_file(init_script_path,
                             FileUtils.join_paths(self.tmp_payload_folder.name,
                                                  FileUtils.get_file_name(init_script_path)))
Пример #2
0
    def create_ecr_image(resources_info: Dict, supervisor_version: str) -> str:
        """Creates an ECR image using the user provided image adding the supervisor tools."""
        # If the user set an already prepared image return the image name
        image_name = ContainerImage._ecr_image_name_prepared(
            resources_info.get('lambda').get('container'))
        if image_name:
            return image_name

        tmp_folder = FileUtils.create_tmp_dir()

        # Create function config file
        FileUtils.write_yaml(
            FileUtils.join_paths(tmp_folder.name, "function_config.yaml"),
            create_function_config(resources_info))

        init_script_path = resources_info.get('lambda').get('init_script')
        # Copy the init script defined by the user to the payload folder
        if init_script_path:
            FileUtils.copy_file(
                init_script_path,
                FileUtils.join_paths(
                    tmp_folder.name,
                    FileUtils.get_file_name(init_script_path)))

        # Get supervisor zip
        supervisor_zip_path = ContainerImage.get_supervisor_zip(
            resources_info, supervisor_version)
        # Unzip the supervisor file to the temp file
        FileUtils.unzip_folder(supervisor_zip_path, tmp_folder.name)

        # Create dockerfile to generate the new ECR image
        FileUtils.create_file_with_content(
            "%s/Dockerfile" % tmp_folder.name,
            ContainerImage._create_dockerfile_ecr_image(
                resources_info.get('lambda')))

        # Create the ECR Repo and get the image uri
        ecr_cli = ECR(resources_info)
        repo_name = resources_info.get('lambda').get('name')
        ecr_image = ecr_cli.get_repository_uri(repo_name)
        if not ecr_image:
            logger.info('Creating ECR repository: %s' % repo_name)
            ecr_image = ecr_cli.create_repository(repo_name)

        # Build and push the image to the ECR repo
        platform = None
        arch = resources_info.get('lambda').get('architectures', ['x86_64'])[0]
        if arch == 'arm64':
            platform = 'linux/arm64'
        return ContainerImage._build_push_ecr_image(
            tmp_folder.name, ecr_image, platform,
            ecr_cli.get_authorization_token())
Пример #3
0
    def _create_dockerfile_ecr_image(lambda_info: Dict) -> str:
        """Create dockerfile to generate the new ECR image."""
        dockerfile = 'from %s\n' % lambda_info.get('container').get('image')
        dockerfile += 'ARG FUNCTION_DIR="/var/task"\n'
        dockerfile += 'WORKDIR ${FUNCTION_DIR}\n'
        dockerfile += 'ENV PATH="${FUNCTION_DIR}:${PATH}"\n'
        # Add PYTHONIOENCODING to avoid UnicodeEncodeError as sugested in:
        # https://github.com/aws/aws-lambda-python-runtime-interface-client/issues/19
        dockerfile += 'ENV PYTHONIOENCODING="utf8"\n'

        # Add user environment variables
        variables = lambda_info.get('container').get('environment').get(
            'Variables', {})
        for key, value in variables.items():
            dockerfile += 'ENV %s="%s"\n' % (key, value)

        dockerfile += 'CMD [ "supervisor" ]\n'
        dockerfile += 'ADD supervisor ${FUNCTION_DIR}\n'
        dockerfile += 'COPY function_config.yaml ${FUNCTION_DIR}\n'
        init_script_path = lambda_info.get('init_script')
        if init_script_path:
            dockerfile += 'COPY %s ${FUNCTION_DIR}\n' % FileUtils.get_file_name(
                init_script_path)
        return dockerfile