能否通过AWS Java SDK创建Lambda后端的REST API?
使用AWS Java SDK创建REST API(支持API密钥等功能)
你之前用的ApiGatewayV2Client是专门用于HTTP API和WebSocket API的,REST API对应的是**ApiGatewayClient**,属于software.amazon.awssdk.services.apigateway包,下面是适配SpringBoot环境的具体实现步骤和代码示例:
1. 引入依赖
在Maven pom.xml中添加AWS API Gateway SDK依赖:
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>apigateway</artifactId> <version>2.x.x</version> <!-- 替换为最新稳定版 --> </dependency>
2. 初始化ApiGatewayClient
在SpringBoot中将客户端配置为Bean,方便注入使用:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import software.amazon.awssdk.services.apigateway.ApiGatewayClient; @Configuration public class AwsConfig { @Bean public ApiGatewayClient apiGatewayClient() { // 可根据需求自定义区域、凭证提供者等配置 return ApiGatewayClient.create(); } }
3. 核心操作代码示例
以下是创建完整REST API(包含资源、API密钥、Lambda集成)的关键代码片段,可直接封装为DevOps服务的对外接口:
创建REST API
import software.amazon.awssdk.services.apigateway.model.CreateRestApiRequest; import software.amazon.awssdk.services.apigateway.model.CreateRestApiResponse; import org.springframework.stereotype.Service; @Service public class ApiGatewayService { private final ApiGatewayClient apiGatewayClient; public ApiGatewayService(ApiGatewayClient apiGatewayClient) { this.apiGatewayClient = apiGatewayClient; } public String createRestApi(String apiName) { CreateRestApiRequest request = CreateRestApiRequest.builder() .name(apiName) .description("DevOps创建的REST API(支持API密钥验证)") .build(); CreateRestApiResponse response = apiGatewayClient.createRestApi(request); return response.id(); // 返回API ID,用于后续资源、方法等操作 } }
创建API资源(如/users路径)
import software.amazon.awssdk.services.apigateway.model.CreateResourceRequest; import software.amazon.awssdk.services.apigateway.model.CreateResourceResponse; public String createResource(String restApiId, String parentResourceId, String pathPart) { CreateResourceRequest request = CreateResourceRequest.builder() .restApiId(restApiId) .parentId(parentResourceId) .pathPart(pathPart) .build(); CreateResourceResponse response = apiGatewayClient.createResource(request); return response.id(); } // 根资源ID可通过getRestApi接口获取,默认值为"root"对应的ID
创建带API密钥验证的方法
import software.amazon.awssdk.services.apigateway.model.PutMethodRequest; public void createMethodWithApiKey(String restApiId, String resourceId, String httpMethod) { PutMethodRequest request = PutMethodRequest.builder() .restApiId(restApiId) .resourceId(resourceId) .httpMethod(httpMethod) .authorizationType("NONE") // 可根据需求替换为AWS_IAM等授权类型 .apiKeyRequired(true) // 开启API密钥验证 .build(); apiGatewayClient.putMethod(request); }
创建Lambda后端集成
import software.amazon.awssdk.services.apigateway.model.PutIntegrationRequest; import software.amazon.awssdk.services.apigateway.model.IntegrationType; import software.amazon.awssdk.regions.Regions; public void createLambdaIntegration(String restApiId, String resourceId, String httpMethod, String lambdaArn, String accountId) { PutIntegrationRequest request = PutIntegrationRequest.builder() .restApiId(restApiId) .resourceId(resourceId) .httpMethod(httpMethod) .type(IntegrationType.AWS_PROXY) .uri(String.format("arn:aws:apigateway:%s:lambda:path/2015-03-31/functions/%s/invocations", Regions.US_EAST_1.id(), lambdaArn)) .credentials(String.format("arn:aws:iam::%s:role/APIGatewayLambdaExecutionRole", accountId)) // 需具备Lambda调用权限 .build(); apiGatewayClient.putIntegration(request); }
创建API密钥并关联使用计划
import software.amazon.awssdk.services.apigateway.model.CreateApiKeyRequest; import software.amazon.awssdk.services.apigateway.model.CreateUsagePlanRequest; import software.amazon.awssdk.services.apigateway.model.CreateUsagePlanKeyRequest; import software.amazon.awssdk.services.apigateway.model.ApiStage; import java.util.List; public String createApiKey(String keyName) { CreateApiKeyRequest request = CreateApiKeyRequest.builder() .name(keyName) .enabled(true) .generateDistinctId(true) .build(); return apiGatewayClient.createApiKey(request).value(); } public String createUsagePlan(String planName, String restApiId, String stageName) { CreateUsagePlanRequest request = CreateUsagePlanRequest.builder() .name(planName) .apiStages(List.of(ApiStage.builder().apiId(restApiId).stage(stageName).build())) .build(); return apiGatewayClient.createUsagePlan(request).id(); } public void linkApiKeyToUsagePlan(String usagePlanId, String apiKeyId) { CreateUsagePlanKeyRequest request = CreateUsagePlanKeyRequest.builder() .usagePlanId(usagePlanId) .keyId(apiKeyId) .keyType("API_KEY") .build(); apiGatewayClient.createUsagePlanKey(request); }
部署API到指定阶段
import software.amazon.awssdk.services.apigateway.model.CreateDeploymentRequest; public void deployApi(String restApiId, String stageName) { CreateDeploymentRequest request = CreateDeploymentRequest.builder() .restApiId(restApiId) .stageName(stageName) .build(); apiGatewayClient.createDeployment(request); }
关键注意事项
- 权限配置:运行SpringBoot应用的IAM角色需具备
apigateway:*(或更细粒度)权限,Lambda调用角色需具备lambda:InvokeFunction权限 - API密钥生效条件:必须开启方法的
apiKeyRequired,同时将API密钥关联到包含目标API阶段的使用计划,否则验证不会生效 - 区域一致性:客户端初始化时需指定目标AWS区域,避免跨区域调用引发的异常
内容的提问来源于stack exchange,提问作者Sandeep
相关产品推荐
相关产品推荐

