2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Today, I will introduce how to design and build Amazon Web Services (AWS) cloud-native architecture using IaC (Infrastructure as Code). This article will introduce how to build cloud-native Serverless services on Amazon Web Services, introduce the development services used, and show the cdk code for building cloud-native architecture. I will also share with you free learning resources for quickly learning popular and cutting-edge technologies on Amazon Web Services, and you can get a certificate after learning.
AWS Lambda is a serverless computing service that allows users to run code without provisioning or managing servers. Users can run the service by simply uploading code and setting upstream triggers. Lambda will then automatically execute the code when an event is triggered, and you only need to pay for the execution time of the code. Lambda supports multiple programming languages, such as Node.js, Python, Java, etc., and can be seamlessly integrated with other AWS services (such as S3, DynamoDB, Kinesis, etc.), making it an ideal choice for building event-driven applications.
Amazon API Gateway is a fully managed service that enables developers to easily create, publish, maintain, monitor, and protect APIs. API Gateway supports RESTful APIs and WebSocket APIs, and can be integrated with AWS Lambda, HTTP endpoints, AWS services, and other backend systems. With API Gateway, users can create secure, scalable APIs and take advantage of built-in traffic control, monitoring, and version management features to simplify API management.
Amazon Simple Queue Service (SQS) is a fully managed message queue service (Cloud Message Queue) designed to decouple and scale microservices, distributed systems, and serverless applications. SQS provides two types of queues: standard queues, which guarantee high throughput but not message order, and FIFO queues, which guarantee strict ordering of messages and on-demand processing. With SQS, you can reliably send, store, and receive messages, ensuring loose coupling and high availability between different system components.
After understanding the mainstream cloud native services, let's learn how to define and create Amazon Web Services (IaC) cloud infrastructure using code. The tool we use is AWS SDK, and the scripting language is typescript, a special language for AWS CDK.
AWS Cloud Development Kit (AWS CDK) is an open source software development framework that allows developers to define cloud infrastructure using familiar programming languages. The emergence of CDK makes it easier and more efficient to create and manage AWS resources. By using AWS CDK, developers can write infrastructure in the form of code, thus implementing the practice of Infrastructure as Code (IaC).
To install CDK, you need to install a series of dependencies, including Node.js, npm, and aws cli. After installing the above dependencies, follow the specific steps below:
npm install -g aws-cdk
cdk --version
- mkdir my-cdk
- cd my-cdk
- cdk init app --language typescript
npm install @aws-cdk/aws-ec2 @aws-cdk/core
Main application file (app.ts)
- import * as cdk from '@aws-cdk/core';
- import { SqsStack } from './sqs-stack';
- import { LambdaStack } from './lambda-stack';
- import { ApiGatewayStack } from './api-gateway-stack';
-
- const app = new cdk.App();
-
- const sqsStack = new SqsStack(app, 'SqsStack');
- const lambdaStack = new LambdaStack(app, 'LambdaStack', sqsStack);
- new ApiGatewayStack(app, 'ApiGatewayStack', lambdaStack);
-
- app.synth();
Create a Lambda function
- import * as cdk from '@aws-cdk/core';
- import * as lambda from '@aws-cdk/aws-lambda';
-
- export class LambdaStack extends cdk.Stack {
- constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
- super(scope, id, props);
-
- // 创建 Lambda 函数
- const myLambda = new lambda.Function(this, 'MyLambda', {
- runtime: lambda.Runtime.NODEJS_14_X,
- handler: 'index.handler',
- code: lambda.Code.fromAsset('lambda'),
- });
- }
- }
Create an API Gateway
- import * as cdk from '@aws-cdk/core';
- import * as apigateway from '@aws-cdk/aws-apigateway';
- import { LambdaStack } from './lambda-stack';
-
- export class ApiGatewayStack extends cdk.Stack {
- constructor(scope: cdk.Construct, id: string, lambdaStack: LambdaStack, props?: cdk.StackProps) {
- super(scope, id, props);
-
- // 创建 API Gateway REST API 并与 Lambda 集成
- const api = new apigateway.RestApi(this, 'MyApi', {
- restApiName: 'My Service',
- description: 'This service serves as an example.',
- });
-
- const lambdaIntegration = new apigateway.LambdaIntegration(lambdaStack.myLambda, {
- requestTemplates: { 'application/json': '{ "statusCode": "200" }' }
- });
-
- api.root.addMethod('GET', lambdaIntegration); // GET / 触发 Lambda
- }
- }
Creating an SQS Queue
- import * as cdk from '@aws-cdk/core';
- import * as sqs from '@aws-cdk/aws-sqs';
-
- export class SqsStack extends cdk.Stack {
- public readonly myQueue: sqs.Queue;
-
- constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
- super(scope, id, props);
-
- // 创建 SQS 队列
- this.myQueue = new sqs.Queue(this, 'MyQueue', {
- visibilityTimeout: cdk.Duration.seconds(300)
- });
- }
- }
本次课程和证书是关于目前云上开发最🔥的Serverless无服务器开发,Serverless服务说白了就是一台服务器,大家可以部署写好的代码,但是服务器是由AWS帮忙维护的,减轻了基础设施维护压力,而且基础设施可以根据并发请求数量自动扩容,保证系统性能。证书名字叫AWS Educate Getting Start with Serverless,内含免费课程和经典AWS实验(没听错,免费给你用AWS做实验,通过10道测试题后拿证书(5分钟就能拿到)。
Brother Li has also prepared the question bank for all 12 AWS certificate exams and the question bank for this certificate. Please follow Brother Li’s private message to get it.
It is a free program for students and AWS beginners to help them learn and master AWS. It includes hundreds of hours of courses + free experiments. The experiments are in a real AWS environment. You don't need to pay to create AWS resources yourself. It is highly recommended. In addition to this developer certificate, there are 9 other free certificates on AWS☁️Basics, Web System Development, Database, Network, DevOps, Security, DeepRacer (AWS driverless service). Follow Brother Li and get 52 AWS certifications!
Including the most popular knowledge points of serverless development
▶️ Enter the course from the home page
▶️ Enter the login interface, enter your account password and log in
▶️ Click on the DashBoard on the left column of the picture, and click on the red box to enter the course
▶️ The course includes 1 video and 1 experiment (you can skip it by clicking Next)
▶️ You can get the certificate after passing the Final Assessment (10 questions, 70% pass)