When I shared a long long link, I wanted to shorten it, so after a lot of research, I found some tutorials. I made a URL shortening service with AWS Cloud Development Kit (CDK) A story about building a URL shortening service that can be used in-house at low cost without a server I created it in the latter tutorial once, but I was disgusted with the fact that CloudFront's custom domain setting did not work and I was sick of popping the console, so I ended up customizing it based on the former tutorial.
What is AWS CDK Simply put, it is a tool that allows you to create the resources you need on AWS based on files written in a familiar language. When writing CloudFormation directly, create a configuration file with Json or Yaml, but you can replace it with Python or Typescript. It is necessary to set some IAMs appropriately, but I personally like it because I can write it as if I were saying it. However, the bottleneck is that there are not many samples, and if you are not familiar with AWS terms and resource-related mechanisms, you will not be sluggish. (Personally, I had a hard time because I wasn't used to touching Lambda or Api Gateway.) CDK API list
The introduction of cdk and the creation of the base stack are the same as in the Manma tutorial, so I will omit them. Please refer to the following. I made a URL shortening service with AWS Cloud Development Kit (CDK)
url_shortener_stack.py
api = aws_apigateway.LambdaRestApi(self, "UrlShortenerApi", handler=handler)
I couldn't find how to lock the API Gateway when using LambdaRestApi
, so I implemented it usingRestApi
instead.
url_shortener_stack.py
api = aws_apigateway.RestApi(self, "UrlShortenerApi")
shorten_integration = aws_apigateway.LambdaIntegration(shorten_handler)
redirect_integration = aws_apigateway.LambdaIntegration(redirect_handler)
redirect = api.root.add_resource('{proxy}')
redirect_method = redirect.add_method("GET", redirect_integration)#No key
shorten = api.root.add_resource("shorten")
shorten_method = shorten.add_method("GET", shorten_integration, api_key_required=True)#With key
api_key = api.add_api_key('APIKey',api_key_name='BuildAPIKey')#Key creation
plan = api.add_usage_plan('ForAPIKey', api_key=api_key, throttle={
"rate_limit": 100,
"burst_limit": 1000
})#Usage plan creation, key specification
plan.add_api_stage(stage=api.deployment_stage,
throttle=[
{
"method": shorten_method,
"throttle": {
"rate_limit": 100,
"burst_limit": 1000
}
},
{
"method": redirect_method,
"throttle": {
"rate_limit": 100,
"burst_limit": 1000
}
},
]
)#Linking the stage and plan
self.map_company_subdomain('go', api)
In the original sample, it was controlled by Lambda's main function, but it could not be controlled nicely due to lack of power (because the main of lambda fires when the proxy has an arbitrary token, appropriate / token? If you access it in the form of targetUrl =, you can fire the shortening function from the unlocked {proxy} root), make the shortening function and the redirecting function different Lambda functions, and then set each , Linked to APIGateway method and locked abbreviated function.
If you keep the tutorial, it will be returned in text / plain, so change to json
lambda/shorten.py
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps({'shrink_url': url})
}
You don't have to go to the console one by one, and cdk diff is convenient for the difference, so I want to use it positively, but I can't write appropriate code unless I have enough knowledge of the underlying AWS resources. This code works for the time being, but I think there are many points that cannot be reached such as API Gateway stage, throttling, keys, integration and control of Lamda functions, so I thought that I should improve it gradually.
Recommended Posts