Part 11. AWS at a glance - API Gateway, CICD

· Tech· Cloud
CloudCertification

API Gateway

You can give users access through API Gateway. It provides API-specific functions, provides authentication and authorization, manages API versions, and supports serverless (ex. Lambda). There are several ways to deploy, such as placing an endpoint in one region and enabling access from around the world using Edge-Optimized, using CloudFront in one region, or using ENI in a VPC. For security, there are methods that use IAM, Cognito, and Custom authorizer.

  • Open API spec can be used. (aka. Swagger) Can be used for validation check.
  • You can limit or monitor usage by using x-api-key in the header. This is because you are connected to using the API associated with a specific usage plan.
  • Logging and tracking are possible using CloudWatchLog(CacheHitCount, CacheMissCount) and X-Ray.

APIs

Rest API, HTTP API, WebSocket API

Rest API vs HTTP API

Rest API cannot be connected using OAuth 2.0 or OpenID. HTTP API has low latency and low cost. However, an API key according to the usage plan is not provided.

WebSocket API

This is necessary for developing applications such as chat that enable two-way communication and require real-time operation. When sending a message to the client, ConnectionID is used. This connection ID is used.

wss://abcdefg123.execute-api.us-east-1.amazonaws.com/dev
wss://abcdefg123.execute-api.us-east-1.amazonaws.com/dev/@connections/{connectionId}

###Stage

  • You can manage it by calling a specific Lambda according to Stage Variables.
  • Canary Deployment
  • Used for testing small amounts of traffic.
  • You can separate metrics and logs by setting the traffic ratio between Canary and Current stage in Canary Setting.
  • If you do a Promote canary, you can promote it to the current Stage with a new version as an update.

Mapping Template

It is used to convert input data or output data to be mapped. It is used to send requests to the backend or transform the response received from the backend. It is written using VTL (Velocity Template Language). SOAP API is based on XML, and REST API is based on JSON.

{
"id": "$input.json('$.userId')",
"name": "$input.json('$.userName')"
}

###Caching The TTL defaults to 300 seconds as a way to reduce backend calls. Cache settings can be set for each stage, and the capacity ranges from 0.5GB to 237GB.

  • You can invalidate the cache on the client by setting Cache-Control: max-age=0 in the header.

Errors

4xx: Client Errors

  • 400 Bad Request: Bad request format.
  • 401 Unauthorized: Authentication failed (no API Key, etc.).
  • 403 Forbidden: Insufficient permission.
  • 404 Not Found: Requested resource not found.
  • 405 Method Not Allowed: Use of an HTTP method that is not permitted.
  • 406 Not Acceptable: The requested format is not supported.
  • 408 Request Timeout: Request timed out.
  • 429 Too Many Requests: Number of calls exceeded (Rate Limiting).

5xx: Server Errors

  • 500 Internal Server Error: Internal server error.
  • 502 Bad Gateway: Bad response from backend service.
  • 503 Service Unavailable: Service is temporarily unavailable.
  • 504 Gateway Timeout: Backend service timeout.

Routing

This is the process of delivering the message to a specific endpoint or Lambda function when the server processes the message sent by the client. You can use an expression to route in a JSON message as follows; for WebSocket-based APIs, the expression must be in the format $request.body.{path_to_body_element}. In the expression below, $request.body.action maps a variable called join.

{
"service" : "chat",
"action" : "join",
"data" : {
"room" : "room1234"
}
}

Unlike a mapping template, it specifies which path the message will take, which is different from changing the request and response like a mapping template.

CICD (Continuous Integration, Continuous Deliver)

CodeCommit, CodePipeline, CodeBuild, CodeDeploy, CodeStar, CodeArtifact, CodeGuru Learn how to automate code management and deployment on AWS. Each CICD-related service provides the following functions.

  • CodeCommit – Saves the code.
  • CodePipeline – Automate pipelines that automatically deploy code to Elastic Beanstalk.
  • CodeBuild – Build and test your code.
  • CodeDeploy – Deploy code to EC2 instances.
  • CodeStar – Manage your software development activities in one place.
  • CodeArtifact – Store, publish, and share software packages.
  • CodeGuru – Provides automatic code review using machine learning.

CodeCommit

Support discontinued on 2024-07-25 It is used for version control, and it is desirable to have a Git Repo online to enable collaboration. Having your code managed by AWS provides security and full management. Although Git is used, security is handled using SSH keys, HTTPS, and IAM for authentication and authorization. In this way, it differs from Github in terms of security and hosting entity.

CodePipeline

This is a service that visualizes the CICD workflow and allows you to review a series of or parallel tasks such as build, test, and deployment in the stage pipeline. It operates under the pipeline by storing artifacts from each process in S3 and sending them to the next stage.

CodeBuild

It is literally used to build code and requires buildspec.yml file in the root of the code file. Run CodeBuild Container according to this file and import the required Docker image. You can then file cache the final artifact in an S3 bucket. buildsepc.yml example is as follows:

version: 0.2
run-as: Linux-user-name
env:
shell: shell-tag
variables:
key: "value"
parameter-store:
key: "value"
exported-variables:
- variable
secrets-manager:
key: secret-id:json-key:version-stage:version-id
git-credential-helper: no | yes
proxy:
upload-artifacts: no | yes
logs: no | yes
batch:
fast-fail: false | true
# build-list:
# build-matrix:
# build-graph:
phases:
install:
run-as: Linux-user-name
on-failure: ABORT | CONTINUE
runtime-versions:
runtime: version
commands:
- command
finally:
- command
pre_build:
run-as: Linux-user-name
on-failure: ABORT | CONTINUE
commands:
- command
finally:
- command
build:
run-as: Linux-user-name
on-failure: ABORT | CONTINUE
commands:
- command
finally:
- command
post_build:
run-as: Linux-user-name
on-failure: ABORT | CONTINUE
commands:
- command
finally:
- command
reports:
report-group-name-or-arn:
files:
- location
base-directory: location
discard-paths: no | yes
file-format: report-format
artifacts:
files:
- location
name: artifact-name
discard-paths: no | yes
base-directory: location
exclude-paths: excluded paths
enable-symlinks: no | yes
s3-prefix: prefix
secondary-artifacts:
artifactIdentifier:
files:
- location
name: secondary-artifact-name
discard-paths: no | yes
base-directory: location
cache:
paths:
- path

CodeDeploy

When you want to deploy your app, you might consider using EC2 instances, Lambda, or ECS services. It enables safe deployment automation, such as limiting the deployment speed or providing a rollback function when problems occur during deployment. To apply CodeDeploy, there must be an appspec.yml file that records settings during deployment.

version: 0.0
os: linux
files:
- source: /app/
destination: /var/www/myapp/
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 180
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 180
runas: root
ApplicationStart:
- location: scripts/application_start.sh
timeout: 180
runas: root
ValidateService:
- location: scripts/validate_service.sh
timeout: 180
runas: root
  • When deploying EC2 instances
  • CodeDeploy Agent with IAM permission downloads the source from the S3 bucket and deploys it.
  • Using ASG, you can use In-Place deployment (updating existing instances) and Blue/Green deployment (creating and deploying a new environment).
  • When deploying Lambda
  • Shift traffic by increasing the percentage of new versions for gradual deployment. There is a linear method to increase it like 10PercentEvery10Minute, a canary method, and an AllAtOnce strategy to move it all at once.
  • When deploying ECS Cluster
  • When using ECS, create a new version of the Target Group in the ECS Cluster and change traffic to the new version using the Linear, Canary, and AllAtOnce strategies, just like Lambda. When a rollback occurs in AWS CodeDeploy, CodeDeploy reapplies the revision of the last successful deployment as a new deployment. In other words, rather than simply restoring the previously deployed version, the last normally deployed revision is processed as a new deployment.

CodeArtifacts

It exists within a VPC to maintain code dependencies and can be safely managed by pre-storing packages within CodeArtifacts rather than relying on external storage.

CodeGuru

We provide services for code review and code improvement. We analyze possible risks, bugs, and security vulnerabilities and provide recommendations. Supports Java and Python.

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164