DevOps on AWS: Complete Guide
Discover the AWS services for DevOps automation and orchestration.
I
InSkillCoach
· min
DevOps on AWS: Complete Guide
Discover the AWS services for DevOps automation and orchestration.
1. CI/CD with AWS CodePipeline
Pipeline Configuration
# Example CodePipeline configuration
import boto3
def create_codepipeline():
codepipeline = boto3.client('codepipeline')
pipeline_config = {
'name': 'MonPipeline',
'roleArn': 'arn:aws:iam::account:role/service-role/AWSCodePipelineServiceRole',
'stages': [
{
'name': 'Source',
'actions': [
{
'name': 'Source',
'actionTypeId': {
'category': 'Source',
'owner': 'AWS',
'provider': 'CodeCommit',
'version': '1'
},
'configuration': {
'RepositoryName': 'mon-repo',
'BranchName': 'main'
},
'outputArtifacts': [
{
'name': 'SourceOutput'
}
]
}
]
},
{
'name': 'Build',
'actions': [
{
'name': 'Build',
'actionTypeId': {
'category': 'Build',
'owner': 'AWS',
'provider': 'CodeBuild',
'version': '1'
},
'configuration': {
'ProjectName': 'MonProjetBuild'
},
'inputArtifacts': [
{
'name': 'SourceOutput'
}
],
'outputArtifacts': [
{
'name': 'BuildOutput'
}
]
}
]
}
]
}
response = codepipeline.create_pipeline(**pipeline_config)
return response['pipeline']['name']
CodeBuild Configuration
# Example CodeBuild configuration
def setup_codebuild():
codebuild = boto3.client('codebuild')
project_config = {
'name': 'MonProjetBuild',
'source': {
'type': 'CODECOMMIT',
'location': 'https://git-codecommit.region.amazonaws.com/v1/repos/mon-repo',
'gitCloneDepth': 1
},
'artifacts': {
'type': 'NO_ARTIFACTS'
},
'environment': {
'type': 'LINUX_CONTAINER',
'image': 'aws/codebuild/amazonlinux2-x86_64-standard:4.0',
'computeType': 'BUILD_GENERAL1_SMALL'
},
'serviceRole': 'arn:aws:iam::account:role/service-role/AWSCodeBuildServiceRole'
}
response = codebuild.create_project(**project_config)
return response['project']['name']
2. Infrastructure as Code with AWS CloudFormation
Creating a Stack
# Example of creating a CloudFormation stack
def create_cloudformation_stack():
cloudformation = boto3.client('cloudformation')
template = {
'AWSTemplateFormatVersion': '2010-09-09',
'Description': 'Stack de développement',
'Resources': {
'MyEC2Instance': {
'Type': 'AWS::EC2::Instance',
'Properties': {
'InstanceType': 't2.micro',
'ImageId': 'ami-0c55b159cbfafe1f0',
'Tags': [
{
'Key': 'Name',
'Value': 'InstanceDev'
}
]
}
}
}
}
response = cloudformation.create_stack(
StackName='MonStackDev',
TemplateBody=json.dumps(template),
Parameters=[
{
'ParameterKey': 'Environment',
'ParameterValue': 'Development'
}
]
)
return response['StackId']
Updating a Stack
# Example of updating a stack
def update_cloudformation_stack():
cloudformation = boto3.client('cloudformation')
template = {
'AWSTemplateFormatVersion': '2010-09-09',
'Description': 'Stack de développement mis à jour',
'Resources': {
'MyEC2Instance': {
'Type': 'AWS::EC2::Instance',
'Properties': {
'InstanceType': 't2.small',
'ImageId': 'ami-0c55b159cbfafe1f0',
'Tags': [
{
'Key': 'Name',
'Value': 'InstanceDev'
}
]
}
}
}
}
response = cloudformation.update_stack(
StackName='MonStackDev',
TemplateBody=json.dumps(template)
)
return response['StackId']
3. Container Management with Amazon ECS
Cluster Configuration
# Example ECS configuration
def setup_ecs_cluster():
ecs = boto3.client('ecs')
# Create the cluster
cluster = ecs.create_cluster(
clusterName='MonCluster',
capacityProviders=['FARGATE'],
defaultCapacityProviderStrategy=[
{
'capacityProvider': 'FARGATE',
'weight': 1
}
]
)
# Define the task
task_definition = ecs.register_task_definition(
family='MonApplication',
networkMode='awsvpc',
requiresCompatibilities=['FARGATE'],
cpu='256',
memory='512',
executionRoleArn='arn:aws:iam::account:role/ecsTaskExecutionRole',
containerDefinitions=[
{
'name': 'MonContainer',
'image': 'mon-image:latest',
'portMappings': [
{
'containerPort': 80,
'protocol': 'tcp'
}
],
'essential': True
}
]
)
return {
'cluster_arn': cluster['cluster']['clusterArn'],
'task_definition_arn': task_definition['taskDefinition']['taskDefinitionArn']
}
Service Deployment
# Example of deploying an ECS service
def deploy_ecs_service():
ecs = boto3.client('ecs')
service = ecs.create_service(
cluster='MonCluster',
serviceName='MonService',
taskDefinition='MonApplication',
desiredCount=2,
launchType='FARGATE',
networkConfiguration={
'awsvpcConfiguration': {
'subnets': ['subnet-xxxxx'],
'securityGroups': ['sg-xxxxx'],
'assignPublicIp': 'ENABLED'
}
}
)
return service['service']['serviceArn']
4. Monitoring with AWS CloudWatch
Metrics Configuration
# Example CloudWatch configuration
def setup_cloudwatch_monitoring():
cloudwatch = boto3.client('cloudwatch')
# Create an alarm
alarm = cloudwatch.put_metric_alarm(
AlarmName='CPUUtilization',
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistic='Average',
Period=300,
EvaluationPeriods=1,
Threshold=80,
ComparisonOperator='GreaterThanThreshold',
AlarmActions=['arn:aws:sns:region:account:MonTopic']
)
# Configure logs
log_group = cloudwatch.create_log_group(
logGroupName='/aws/ecs/MonApplication',
retentionInDays=30
)
return {
'alarm_name': alarm['AlarmArn'],
'log_group': log_group['logGroupName']
}
Dashboard Configuration
# Example of creating a dashboard
def create_cloudwatch_dashboard():
cloudwatch = boto3.client('cloudwatch')
dashboard_body = {
'widgets': [
{
'type': 'metric',
'x': 0,
'y': 0,
'width': 12,
'height': 6,
'properties': {
'metrics': [
['AWS/EC2', 'CPUUtilization', 'InstanceId', 'i-xxxxx']
],
'period': 300,
'stat': 'Average',
'region': 'region',
'title': 'Utilisation CPU'
}
}
]
}
response = cloudwatch.put_dashboard(
DashboardName='MonDashboard',
DashboardBody=json.dumps(dashboard_body)
)
return response['DashboardArn']
5. Secrets Management with AWS Secrets Manager
Secrets Configuration
# Example of secrets management
def manage_secrets():
secretsmanager = boto3.client('secretsmanager')
# Create a secret
secret = secretsmanager.create_secret(
Name='MonSecret',
SecretString=json.dumps({
'username': 'admin',
'password': 'secret123'
}),
Description='Secret pour l\'application'
)
# Retrieve the secret
get_secret = secretsmanager.get_secret_value(
SecretId='MonSecret'
)
return {
'secret_arn': secret['ARN'],
'secret_value': json.loads(get_secret['SecretString'])
}
Secrets Rotation
# Example of secrets rotation
def rotate_secret():
secretsmanager = boto3.client('secretsmanager')
# Configure rotation
rotation = secretsmanager.rotate_secret(
SecretId='MonSecret',
RotationRules={
'AutomaticallyAfterDays': 30
}
)
return rotation['ARN']
Conclusion
Key takeaways:
- Automate deployment with CodePipeline
- Manage infrastructure with CloudFormation
- Orchestrate containers with ECS
- Monitor with CloudWatch
- Secure your secrets
Recommendations:
- Use infrastructure as code
- Set up CI/CD pipelines
- Automate deployments
- Monitor performance
- Manage secrets securely
About InSkillCoach
Training and technology expert
Coach specializing in advanced technologies and AI, backed by GNeurone Inc.
Certifications:
- AWS Certified Solutions Architect – Professional
- Google Cloud certifications
- Microsoft Certified: DevOps Engineer Expert
- Certified Kubernetes Administrator (CKA)
- CompTIA Security+
1.4k
335
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion