본문 바로가기

Server Infra/AWS

AWS Lambda Function URL 사용 #1

728x90

https://aws.amazon.com/ko/blogs/architecture/deploy-quarkus-based-applications-using-aws-lambda-with-aws-sam/

 

Deploy Quarkus-based applications using AWS Lambda with AWS SAM | Amazon Web Services

­Quarkus offers Java developers the capability of building native images based on GraalVM. A native image is a binary that includes everything: your code, libraries, and a smaller virtual machine (VM). This approach improves the startup time of your AWS L

aws.amazon.com

https://aws.amazon.com/ko/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/

 

Announcing AWS Lambda Function URLs: Built-in HTTPS Endpoints for Single-Function Microservices | Amazon Web Services

Organizations are adopting microservices architectures to build resilient and scalable applications using AWS Lambda. These applications are composed of multiple serverless functions that implement the business logic. Each function is mapped to API endpoin

aws.amazon.com

최근 람다에 자체 함수 URL이 생겼다는 이야기를 들었다.

기존에 API Gateway나 ALB를 이용해 호출하던게 자체적으로 Domain을 가지게 된것이다. 물론 API gw나 ALB를 사용한다면 보안성이 좀더 높아질 것이다. WAF연동도 가능하고 기타 헤더조건을 활용한 접근 제한등 다양한 것을 활용 할 수 있기 때문. 물론, 함수 URL을 사용할때도 몇가지 인증 유형을 지원한다

우선 간략하게 테스트 None부터 테스트를 해보겠다

기본 함수를 만들고 함수 URL을 활성화 해준다. 그리고 curl로 호출하면

API gw나 ALB없이 바로 람다가 호출된다.

Lambda는 REST와 HTTP 두가지를 지원한다. 우선 HTML을 한번 해보자

import json

def lambda_handler(event, context):
    response_body = "<html><head><title>HTML from API Gateway/Lambda</title></head><body><h1>HTML from API Lambda</h1></body></html>"
    # TODO implement
    return {
    "statusCode": 200,
    "body": response_body,
    "headers": {
        'Content-Type': 'text/html',
    }
}

위처럼 간략하게 Python코드를 만들어 호출하면

HTML tag가 잘 인식된다. 그렇다면 SSR도 충분히 가능하다는것. 

※다만 HTML은 AWS_IAM인증을 지원하지 않기에 CORS나 코드내 인증 절차를 마련하는게 좋다. 아직까지 WAF나 별다른 보안 조치가 존재하지 않다.

 

그러면 이제 AWS_IAM으로 테스트 해보자

Lambda의 여러 탭중 함수URL로 되어 있는 사이드 바를 클릭하면 바로 편집이 가능하다. 같은 코드로 변경해 보겠다.

테스트 할겸 EC2하나를 생성해 CLI로 Invoke를 시도해 보았다. 당연히 실패다.

그러면 Lambda에 리소그 기반 정책에 Principal로 EC2의 프로파일로 사용하는 IAM Role을 명시해 보겠다.

당연히 잘된다.

 

그러면 VPC를 활성화 하고 Private존에서 호출하면 잘 될까??? 그건 다음 포스팅으로...

728x90