Skip to content

Instantly share code, notes, and snippets.

@fclesio
Last active January 5, 2022 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fclesio/7196e36a070900074858f1a3ed7d084f to your computer and use it in GitHub Desktop.
Save fclesio/7196e36a070900074858f1a3ed7d084f to your computer and use it in GitHub Desktop.
Create a Job Definition using Fargate setting resourceRequirements instead computeResources
import os
import boto3
COMPUTE_ENVIRONMENT_VERSION = 'v0_01'
aws_iam_boto_client \
= boto3.client('iam')
aws_batch_boto_client \
= boto3.client('batch')
application_service_role \
= iam_boto_client.get_role(
RoleName='your_generic_executon_role'
)
def job_definition_create(
aws_batch_boto_client=aws_batch_boto_client,
version=COMPUTE_ENVIRONMENT_VERSION,
):
"""Create a job definition in AWS Batch using FARGATE
setting resourceRequirements instead computeResources,
in a more clear manner than the Boto3 documentation
Parameters
----------
aws_batch_boto_client : Boto3 client object
Boto3 client related with the AWS Batch service
version : str
Version related with the Compute Environment
that will follow all downstream resources
as job definitions, jobs queues and jobs.
Returns
-------
dict
{
"ResponseMetadata":{
"RequestId":"00000000-0000-0000-0000-0000000000",
"HTTPStatusCode":200,
"HTTPHeaders":{
"date":"Sat, 01 Jan 2022 00:00:00 GMT",
"content-type":"application/json",
"content-length":"171",
"connection":"keep-alive",
"x-amzn-requestid":"00000000-0000-0000-0000-0000000000",
"access-control-allow-origin":"*",
"x-amz-apigw-id":"ABCDEFGHIJLMNO=",
"access-control-expose-headers":"X-amzn-errortype,X-amzn-requestid,X-amzn-errormessage,X-amzn-trace-id,X-amz-apigw-id,date",
"x-amzn-trace-id":"Root=1-00000000-0000-0000-0000-0000000000"
},
"RetryAttempts":0
},
"jobDefinitionName":"ml_batch_prediction_job_definition_v0_01",
"jobDefinitionArn":"arn:aws:batch:sa-east-1:012345678910:job-definition/ml_batch_prediction_job_definition_v0_01:1",
"revision":1
}
"""
aws_response = aws_batch_boto_client.register_job_definition(
jobDefinitionName=f'ml_batch_prediction_job_definition_{version}',
type='container',
platformCapabilities=['FARGATE'],
containerProperties={
'image': '012345678910.dkr.ecr.sa-east-1.amazonaws.com/ml_batch_prediction_:latest',
'resourceRequirements': [
{
'value': '64000',
'type': 'MEMORY'
},
{
'value': '8',
'type': 'VCPU'
}
],
'networkConfiguration': {
'assignPublicIp': 'ENABLED'
},
'command': [
'python',
'main.py'
],
'jobRoleArn': application_service_role['Role']['Arn'],
'executionRoleArn': application_service_role['Role']['Arn'],
'environment': [
{
'name': 'DB_USER',
'value': f"{os.environ.get('DB_USER')}",
},
{
'name': 'DB_HOST',
'value': f"{os.environ.get('DB_HOST')}",
},
{
'name': 'DB_PASS',
'value': f"{os.environ.get('DB_PASS')}",
},
{
'name': 'DB_PORT',
'value': f"{os.environ.get('DB_PORT')}",
},
{
'name': 'DB_NAME',
'value': f"{os.environ.get('DB_NAME')}",
},
{
'name': 'S3_BUCKET',
'value': f"{os.environ.get('S3_BUCKET')}",
},
],
},
)
print(aws_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment