Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0

j3tm0t0/ddns.lua Secret

Last active December 22, 2019 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j3tm0t0/ce85fafe4f36736767b6f2b4654627f5 to your computer and use it in GitHub Desktop.
Save j3tm0t0/ce85fafe4f36736767b6f2b4654627f5 to your computer and use it in GitHub Desktop.
lua script and lambda function to update Route53 record
-- 設定項目
wan_if = "pp 1"
hosted_zone_id = "Z1234567890ABC"
name = "ddns.example.com"
-- SORACOM Funk 呼び出し
function funk(payload)
req_table = {
url = "http://funk.soracom.io/",
method = "POST",
post_text = payload,
content_type= 'application/json'
}
rsp_t = rt.httprequest(req_table)
debug(tostring(rsp_t.code)..":"..rsp_t.body)
end
function debug(message)
-- print(message)
rt.syslog("info", message)
end
function get_if_addr(interface)
local rtn, str, addr
local cmd = "show status " .. interface
local ptn = "PP IP Address Local: ([%d%.]+),"
rtn, str = rt.command(cmd)
addr = string.match(str,ptn,1)
return addr
end
local addr, sleep_time
sleep_time=0
for i=1, 5 do
addr = get_if_addr(wan_if)
if (addr) then
break
end
sleep_time = sleep_time + i
debug(string.format('could not get interface address for "%s". retry after %d sec.', wan_if, sleep_time))
rt.sleep(sleep_time)
end
if (addr) then
funk(string.format('{"hosted_zone_id":"%s","name":"%s","ip_address":"%s"}',hosted_zone_id, name, addr))
else
debug(string.format('could not get interface address for "%s". aborting...' , wan_if))
end
import boto3,json
client = boto3.client('route53')
def register(config):
if(config.get('hosted_zone_id') and config.get('name') and config.get('ip_address')):
response = client.change_resource_record_sets(
HostedZoneId=config.get('hosted_zone_id'),
ChangeBatch={
'Changes':[
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': config.get('name'),
'Type': 'A',
'ResourceRecords':[ {'Value': config.get('ip_address')} ],
'TTL': 60
}
}
]
}
)
print(response)
return 'OK' if (response.get('ResponseMetadata').get('HTTPStatusCode') == 200) else 'NG'
else:
return "invalid parameters" + json.dump(config)
def lambda_handler(event, context):
print(json.dumps(event))
return register(event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment